d2667e5dddc3acf4524ae280bee3ac832accdf0f
[sfrench/cifs-2.6.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 void 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         BUG_ON(!pagelist);
783         ceph_pagelist_init(pagelist);
784
785         op->cls.class_name = class;
786         size = strlen(class);
787         BUG_ON(size > (size_t) U8_MAX);
788         op->cls.class_len = size;
789         ceph_pagelist_append(pagelist, class, size);
790         payload_len += size;
791
792         op->cls.method_name = method;
793         size = strlen(method);
794         BUG_ON(size > (size_t) U8_MAX);
795         op->cls.method_len = size;
796         ceph_pagelist_append(pagelist, method, size);
797         payload_len += size;
798
799         osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
800
801         op->indata_len = payload_len;
802 }
803 EXPORT_SYMBOL(osd_req_op_cls_init);
804
805 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
806                           u16 opcode, const char *name, const void *value,
807                           size_t size, u8 cmp_op, u8 cmp_mode)
808 {
809         struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
810                                                       opcode, 0);
811         struct ceph_pagelist *pagelist;
812         size_t payload_len;
813
814         BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
815
816         pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
817         if (!pagelist)
818                 return -ENOMEM;
819
820         ceph_pagelist_init(pagelist);
821
822         payload_len = strlen(name);
823         op->xattr.name_len = payload_len;
824         ceph_pagelist_append(pagelist, name, payload_len);
825
826         op->xattr.value_len = size;
827         ceph_pagelist_append(pagelist, value, size);
828         payload_len += size;
829
830         op->xattr.cmp_op = cmp_op;
831         op->xattr.cmp_mode = cmp_mode;
832
833         ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
834         op->indata_len = payload_len;
835         return 0;
836 }
837 EXPORT_SYMBOL(osd_req_op_xattr_init);
838
839 /*
840  * @watch_opcode: CEPH_OSD_WATCH_OP_*
841  */
842 static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
843                                   u64 cookie, u8 watch_opcode)
844 {
845         struct ceph_osd_req_op *op;
846
847         op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
848         op->watch.cookie = cookie;
849         op->watch.op = watch_opcode;
850         op->watch.gen = 0;
851 }
852
853 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
854                                 unsigned int which,
855                                 u64 expected_object_size,
856                                 u64 expected_write_size)
857 {
858         struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
859                                                       CEPH_OSD_OP_SETALLOCHINT,
860                                                       0);
861
862         op->alloc_hint.expected_object_size = expected_object_size;
863         op->alloc_hint.expected_write_size = expected_write_size;
864
865         /*
866          * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
867          * not worth a feature bit.  Set FAILOK per-op flag to make
868          * sure older osds don't trip over an unsupported opcode.
869          */
870         op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
871 }
872 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
873
874 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
875                                 struct ceph_osd_data *osd_data)
876 {
877         u64 length = ceph_osd_data_length(osd_data);
878
879         if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
880                 BUG_ON(length > (u64) SIZE_MAX);
881                 if (length)
882                         ceph_msg_data_add_pages(msg, osd_data->pages,
883                                         length, osd_data->alignment);
884         } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
885                 BUG_ON(!length);
886                 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
887 #ifdef CONFIG_BLOCK
888         } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
889                 ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
890 #endif
891         } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
892                 ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
893         } else {
894                 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
895         }
896 }
897
898 static u32 osd_req_encode_op(struct ceph_osd_op *dst,
899                              const struct ceph_osd_req_op *src)
900 {
901         if (WARN_ON(!osd_req_opcode_valid(src->op))) {
902                 pr_err("unrecognized osd opcode %d\n", src->op);
903
904                 return 0;
905         }
906
907         switch (src->op) {
908         case CEPH_OSD_OP_STAT:
909                 break;
910         case CEPH_OSD_OP_READ:
911         case CEPH_OSD_OP_WRITE:
912         case CEPH_OSD_OP_WRITEFULL:
913         case CEPH_OSD_OP_ZERO:
914         case CEPH_OSD_OP_TRUNCATE:
915                 dst->extent.offset = cpu_to_le64(src->extent.offset);
916                 dst->extent.length = cpu_to_le64(src->extent.length);
917                 dst->extent.truncate_size =
918                         cpu_to_le64(src->extent.truncate_size);
919                 dst->extent.truncate_seq =
920                         cpu_to_le32(src->extent.truncate_seq);
921                 break;
922         case CEPH_OSD_OP_CALL:
923                 dst->cls.class_len = src->cls.class_len;
924                 dst->cls.method_len = src->cls.method_len;
925                 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
926                 break;
927         case CEPH_OSD_OP_WATCH:
928                 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
929                 dst->watch.ver = cpu_to_le64(0);
930                 dst->watch.op = src->watch.op;
931                 dst->watch.gen = cpu_to_le32(src->watch.gen);
932                 break;
933         case CEPH_OSD_OP_NOTIFY_ACK:
934                 break;
935         case CEPH_OSD_OP_NOTIFY:
936                 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
937                 break;
938         case CEPH_OSD_OP_LIST_WATCHERS:
939                 break;
940         case CEPH_OSD_OP_SETALLOCHINT:
941                 dst->alloc_hint.expected_object_size =
942                     cpu_to_le64(src->alloc_hint.expected_object_size);
943                 dst->alloc_hint.expected_write_size =
944                     cpu_to_le64(src->alloc_hint.expected_write_size);
945                 break;
946         case CEPH_OSD_OP_SETXATTR:
947         case CEPH_OSD_OP_CMPXATTR:
948                 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
949                 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
950                 dst->xattr.cmp_op = src->xattr.cmp_op;
951                 dst->xattr.cmp_mode = src->xattr.cmp_mode;
952                 break;
953         case CEPH_OSD_OP_CREATE:
954         case CEPH_OSD_OP_DELETE:
955                 break;
956         default:
957                 pr_err("unsupported osd opcode %s\n",
958                         ceph_osd_op_name(src->op));
959                 WARN_ON(1);
960
961                 return 0;
962         }
963
964         dst->op = cpu_to_le16(src->op);
965         dst->flags = cpu_to_le32(src->flags);
966         dst->payload_len = cpu_to_le32(src->indata_len);
967
968         return src->indata_len;
969 }
970
971 /*
972  * build new request AND message, calculate layout, and adjust file
973  * extent as needed.
974  *
975  * if the file was recently truncated, we include information about its
976  * old and new size so that the object can be updated appropriately.  (we
977  * avoid synchronously deleting truncated objects because it's slow.)
978  */
979 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
980                                                struct ceph_file_layout *layout,
981                                                struct ceph_vino vino,
982                                                u64 off, u64 *plen,
983                                                unsigned int which, int num_ops,
984                                                int opcode, int flags,
985                                                struct ceph_snap_context *snapc,
986                                                u32 truncate_seq,
987                                                u64 truncate_size,
988                                                bool use_mempool)
989 {
990         struct ceph_osd_request *req;
991         u64 objnum = 0;
992         u64 objoff = 0;
993         u64 objlen = 0;
994         int r;
995
996         BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
997                opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
998                opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
999
1000         req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1001                                         GFP_NOFS);
1002         if (!req) {
1003                 r = -ENOMEM;
1004                 goto fail;
1005         }
1006
1007         /* calculate max write size */
1008         r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
1009         if (r)
1010                 goto fail;
1011
1012         if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1013                 osd_req_op_init(req, which, opcode, 0);
1014         } else {
1015                 u32 object_size = layout->object_size;
1016                 u32 object_base = off - objoff;
1017                 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1018                         if (truncate_size <= object_base) {
1019                                 truncate_size = 0;
1020                         } else {
1021                                 truncate_size -= object_base;
1022                                 if (truncate_size > object_size)
1023                                         truncate_size = object_size;
1024                         }
1025                 }
1026                 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1027                                        truncate_size, truncate_seq);
1028         }
1029
1030         req->r_abort_on_full = true;
1031         req->r_flags = flags;
1032         req->r_base_oloc.pool = layout->pool_id;
1033         req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1034         ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
1035
1036         req->r_snapid = vino.snap;
1037         if (flags & CEPH_OSD_FLAG_WRITE)
1038                 req->r_data_offset = off;
1039
1040         r = ceph_osdc_alloc_messages(req, GFP_NOFS);
1041         if (r)
1042                 goto fail;
1043
1044         return req;
1045
1046 fail:
1047         ceph_osdc_put_request(req);
1048         return ERR_PTR(r);
1049 }
1050 EXPORT_SYMBOL(ceph_osdc_new_request);
1051
1052 /*
1053  * We keep osd requests in an rbtree, sorted by ->r_tid.
1054  */
1055 DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
1056 DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
1057
1058 static bool osd_homeless(struct ceph_osd *osd)
1059 {
1060         return osd->o_osd == CEPH_HOMELESS_OSD;
1061 }
1062
1063 static bool osd_registered(struct ceph_osd *osd)
1064 {
1065         verify_osdc_locked(osd->o_osdc);
1066
1067         return !RB_EMPTY_NODE(&osd->o_node);
1068 }
1069
1070 /*
1071  * Assumes @osd is zero-initialized.
1072  */
1073 static void osd_init(struct ceph_osd *osd)
1074 {
1075         refcount_set(&osd->o_ref, 1);
1076         RB_CLEAR_NODE(&osd->o_node);
1077         osd->o_requests = RB_ROOT;
1078         osd->o_linger_requests = RB_ROOT;
1079         osd->o_backoff_mappings = RB_ROOT;
1080         osd->o_backoffs_by_id = RB_ROOT;
1081         INIT_LIST_HEAD(&osd->o_osd_lru);
1082         INIT_LIST_HEAD(&osd->o_keepalive_item);
1083         osd->o_incarnation = 1;
1084         mutex_init(&osd->lock);
1085 }
1086
1087 static void osd_cleanup(struct ceph_osd *osd)
1088 {
1089         WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
1090         WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1091         WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1092         WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1093         WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
1094         WARN_ON(!list_empty(&osd->o_osd_lru));
1095         WARN_ON(!list_empty(&osd->o_keepalive_item));
1096
1097         if (osd->o_auth.authorizer) {
1098                 WARN_ON(osd_homeless(osd));
1099                 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1100         }
1101 }
1102
1103 /*
1104  * Track open sessions with osds.
1105  */
1106 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1107 {
1108         struct ceph_osd *osd;
1109
1110         WARN_ON(onum == CEPH_HOMELESS_OSD);
1111
1112         osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
1113         osd_init(osd);
1114         osd->o_osdc = osdc;
1115         osd->o_osd = onum;
1116
1117         ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1118
1119         return osd;
1120 }
1121
1122 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1123 {
1124         if (refcount_inc_not_zero(&osd->o_ref)) {
1125                 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1126                      refcount_read(&osd->o_ref));
1127                 return osd;
1128         } else {
1129                 dout("get_osd %p FAIL\n", osd);
1130                 return NULL;
1131         }
1132 }
1133
1134 static void put_osd(struct ceph_osd *osd)
1135 {
1136         dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1137              refcount_read(&osd->o_ref) - 1);
1138         if (refcount_dec_and_test(&osd->o_ref)) {
1139                 osd_cleanup(osd);
1140                 kfree(osd);
1141         }
1142 }
1143
1144 DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1145
1146 static void __move_osd_to_lru(struct ceph_osd *osd)
1147 {
1148         struct ceph_osd_client *osdc = osd->o_osdc;
1149
1150         dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1151         BUG_ON(!list_empty(&osd->o_osd_lru));
1152
1153         spin_lock(&osdc->osd_lru_lock);
1154         list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1155         spin_unlock(&osdc->osd_lru_lock);
1156
1157         osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1158 }
1159
1160 static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1161 {
1162         if (RB_EMPTY_ROOT(&osd->o_requests) &&
1163             RB_EMPTY_ROOT(&osd->o_linger_requests))
1164                 __move_osd_to_lru(osd);
1165 }
1166
1167 static void __remove_osd_from_lru(struct ceph_osd *osd)
1168 {
1169         struct ceph_osd_client *osdc = osd->o_osdc;
1170
1171         dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1172
1173         spin_lock(&osdc->osd_lru_lock);
1174         if (!list_empty(&osd->o_osd_lru))
1175                 list_del_init(&osd->o_osd_lru);
1176         spin_unlock(&osdc->osd_lru_lock);
1177 }
1178
1179 /*
1180  * Close the connection and assign any leftover requests to the
1181  * homeless session.
1182  */
1183 static void close_osd(struct ceph_osd *osd)
1184 {
1185         struct ceph_osd_client *osdc = osd->o_osdc;
1186         struct rb_node *n;
1187
1188         verify_osdc_wrlocked(osdc);
1189         dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1190
1191         ceph_con_close(&osd->o_con);
1192
1193         for (n = rb_first(&osd->o_requests); n; ) {
1194                 struct ceph_osd_request *req =
1195                     rb_entry(n, struct ceph_osd_request, r_node);
1196
1197                 n = rb_next(n); /* unlink_request() */
1198
1199                 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1200                 unlink_request(osd, req);
1201                 link_request(&osdc->homeless_osd, req);
1202         }
1203         for (n = rb_first(&osd->o_linger_requests); n; ) {
1204                 struct ceph_osd_linger_request *lreq =
1205                     rb_entry(n, struct ceph_osd_linger_request, node);
1206
1207                 n = rb_next(n); /* unlink_linger() */
1208
1209                 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1210                      lreq->linger_id);
1211                 unlink_linger(osd, lreq);
1212                 link_linger(&osdc->homeless_osd, lreq);
1213         }
1214         clear_backoffs(osd);
1215
1216         __remove_osd_from_lru(osd);
1217         erase_osd(&osdc->osds, osd);
1218         put_osd(osd);
1219 }
1220
1221 /*
1222  * reset osd connect
1223  */
1224 static int reopen_osd(struct ceph_osd *osd)
1225 {
1226         struct ceph_entity_addr *peer_addr;
1227
1228         dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1229
1230         if (RB_EMPTY_ROOT(&osd->o_requests) &&
1231             RB_EMPTY_ROOT(&osd->o_linger_requests)) {
1232                 close_osd(osd);
1233                 return -ENODEV;
1234         }
1235
1236         peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1237         if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1238                         !ceph_con_opened(&osd->o_con)) {
1239                 struct rb_node *n;
1240
1241                 dout("osd addr hasn't changed and connection never opened, "
1242                      "letting msgr retry\n");
1243                 /* touch each r_stamp for handle_timeout()'s benfit */
1244                 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1245                         struct ceph_osd_request *req =
1246                             rb_entry(n, struct ceph_osd_request, r_node);
1247                         req->r_stamp = jiffies;
1248                 }
1249
1250                 return -EAGAIN;
1251         }
1252
1253         ceph_con_close(&osd->o_con);
1254         ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1255         osd->o_incarnation++;
1256
1257         return 0;
1258 }
1259
1260 static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1261                                           bool wrlocked)
1262 {
1263         struct ceph_osd *osd;
1264
1265         if (wrlocked)
1266                 verify_osdc_wrlocked(osdc);
1267         else
1268                 verify_osdc_locked(osdc);
1269
1270         if (o != CEPH_HOMELESS_OSD)
1271                 osd = lookup_osd(&osdc->osds, o);
1272         else
1273                 osd = &osdc->homeless_osd;
1274         if (!osd) {
1275                 if (!wrlocked)
1276                         return ERR_PTR(-EAGAIN);
1277
1278                 osd = create_osd(osdc, o);
1279                 insert_osd(&osdc->osds, osd);
1280                 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1281                               &osdc->osdmap->osd_addr[osd->o_osd]);
1282         }
1283
1284         dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1285         return osd;
1286 }
1287
1288 /*
1289  * Create request <-> OSD session relation.
1290  *
1291  * @req has to be assigned a tid, @osd may be homeless.
1292  */
1293 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1294 {
1295         verify_osd_locked(osd);
1296         WARN_ON(!req->r_tid || req->r_osd);
1297         dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1298              req, req->r_tid);
1299
1300         if (!osd_homeless(osd))
1301                 __remove_osd_from_lru(osd);
1302         else
1303                 atomic_inc(&osd->o_osdc->num_homeless);
1304
1305         get_osd(osd);
1306         insert_request(&osd->o_requests, req);
1307         req->r_osd = osd;
1308 }
1309
1310 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1311 {
1312         verify_osd_locked(osd);
1313         WARN_ON(req->r_osd != osd);
1314         dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1315              req, req->r_tid);
1316
1317         req->r_osd = NULL;
1318         erase_request(&osd->o_requests, req);
1319         put_osd(osd);
1320
1321         if (!osd_homeless(osd))
1322                 maybe_move_osd_to_lru(osd);
1323         else
1324                 atomic_dec(&osd->o_osdc->num_homeless);
1325 }
1326
1327 static bool __pool_full(struct ceph_pg_pool_info *pi)
1328 {
1329         return pi->flags & CEPH_POOL_FLAG_FULL;
1330 }
1331
1332 static bool have_pool_full(struct ceph_osd_client *osdc)
1333 {
1334         struct rb_node *n;
1335
1336         for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1337                 struct ceph_pg_pool_info *pi =
1338                     rb_entry(n, struct ceph_pg_pool_info, node);
1339
1340                 if (__pool_full(pi))
1341                         return true;
1342         }
1343
1344         return false;
1345 }
1346
1347 static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1348 {
1349         struct ceph_pg_pool_info *pi;
1350
1351         pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1352         if (!pi)
1353                 return false;
1354
1355         return __pool_full(pi);
1356 }
1357
1358 /*
1359  * Returns whether a request should be blocked from being sent
1360  * based on the current osdmap and osd_client settings.
1361  */
1362 static bool target_should_be_paused(struct ceph_osd_client *osdc,
1363                                     const struct ceph_osd_request_target *t,
1364                                     struct ceph_pg_pool_info *pi)
1365 {
1366         bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1367         bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1368                        ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1369                        __pool_full(pi);
1370
1371         WARN_ON(pi->id != t->target_oloc.pool);
1372         return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1373                ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1374                (osdc->osdmap->epoch < osdc->epoch_barrier);
1375 }
1376
1377 enum calc_target_result {
1378         CALC_TARGET_NO_ACTION = 0,
1379         CALC_TARGET_NEED_RESEND,
1380         CALC_TARGET_POOL_DNE,
1381 };
1382
1383 static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1384                                            struct ceph_osd_request_target *t,
1385                                            struct ceph_connection *con,
1386                                            bool any_change)
1387 {
1388         struct ceph_pg_pool_info *pi;
1389         struct ceph_pg pgid, last_pgid;
1390         struct ceph_osds up, acting;
1391         bool force_resend = false;
1392         bool unpaused = false;
1393         bool legacy_change;
1394         bool split = false;
1395         bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1396         bool recovery_deletes = ceph_osdmap_flag(osdc,
1397                                                  CEPH_OSDMAP_RECOVERY_DELETES);
1398         enum calc_target_result ct_res;
1399         int ret;
1400
1401         t->epoch = osdc->osdmap->epoch;
1402         pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1403         if (!pi) {
1404                 t->osd = CEPH_HOMELESS_OSD;
1405                 ct_res = CALC_TARGET_POOL_DNE;
1406                 goto out;
1407         }
1408
1409         if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1410                 if (t->last_force_resend < pi->last_force_request_resend) {
1411                         t->last_force_resend = pi->last_force_request_resend;
1412                         force_resend = true;
1413                 } else if (t->last_force_resend == 0) {
1414                         force_resend = true;
1415                 }
1416         }
1417
1418         /* apply tiering */
1419         ceph_oid_copy(&t->target_oid, &t->base_oid);
1420         ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1421         if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1422                 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1423                         t->target_oloc.pool = pi->read_tier;
1424                 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1425                         t->target_oloc.pool = pi->write_tier;
1426
1427                 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1428                 if (!pi) {
1429                         t->osd = CEPH_HOMELESS_OSD;
1430                         ct_res = CALC_TARGET_POOL_DNE;
1431                         goto out;
1432                 }
1433         }
1434
1435         ret = __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc,
1436                                           &pgid);
1437         if (ret) {
1438                 WARN_ON(ret != -ENOENT);
1439                 t->osd = CEPH_HOMELESS_OSD;
1440                 ct_res = CALC_TARGET_POOL_DNE;
1441                 goto out;
1442         }
1443         last_pgid.pool = pgid.pool;
1444         last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1445
1446         ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
1447         if (any_change &&
1448             ceph_is_new_interval(&t->acting,
1449                                  &acting,
1450                                  &t->up,
1451                                  &up,
1452                                  t->size,
1453                                  pi->size,
1454                                  t->min_size,
1455                                  pi->min_size,
1456                                  t->pg_num,
1457                                  pi->pg_num,
1458                                  t->sort_bitwise,
1459                                  sort_bitwise,
1460                                  t->recovery_deletes,
1461                                  recovery_deletes,
1462                                  &last_pgid))
1463                 force_resend = true;
1464
1465         if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1466                 t->paused = false;
1467                 unpaused = true;
1468         }
1469         legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1470                         ceph_osds_changed(&t->acting, &acting, any_change);
1471         if (t->pg_num)
1472                 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
1473
1474         if (legacy_change || force_resend || split) {
1475                 t->pgid = pgid; /* struct */
1476                 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
1477                 ceph_osds_copy(&t->acting, &acting);
1478                 ceph_osds_copy(&t->up, &up);
1479                 t->size = pi->size;
1480                 t->min_size = pi->min_size;
1481                 t->pg_num = pi->pg_num;
1482                 t->pg_num_mask = pi->pg_num_mask;
1483                 t->sort_bitwise = sort_bitwise;
1484                 t->recovery_deletes = recovery_deletes;
1485
1486                 t->osd = acting.primary;
1487         }
1488
1489         if (unpaused || legacy_change || force_resend ||
1490             (split && con && CEPH_HAVE_FEATURE(con->peer_features,
1491                                                RESEND_ON_SPLIT)))
1492                 ct_res = CALC_TARGET_NEED_RESEND;
1493         else
1494                 ct_res = CALC_TARGET_NO_ACTION;
1495
1496 out:
1497         dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1498         return ct_res;
1499 }
1500
1501 static struct ceph_spg_mapping *alloc_spg_mapping(void)
1502 {
1503         struct ceph_spg_mapping *spg;
1504
1505         spg = kmalloc(sizeof(*spg), GFP_NOIO);
1506         if (!spg)
1507                 return NULL;
1508
1509         RB_CLEAR_NODE(&spg->node);
1510         spg->backoffs = RB_ROOT;
1511         return spg;
1512 }
1513
1514 static void free_spg_mapping(struct ceph_spg_mapping *spg)
1515 {
1516         WARN_ON(!RB_EMPTY_NODE(&spg->node));
1517         WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1518
1519         kfree(spg);
1520 }
1521
1522 /*
1523  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1524  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1525  * defined only within a specific spgid; it does not pass anything to
1526  * children on split, or to another primary.
1527  */
1528 DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1529                  RB_BYPTR, const struct ceph_spg *, node)
1530
1531 static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1532 {
1533         return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1534 }
1535
1536 static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1537                                    void **pkey, size_t *pkey_len)
1538 {
1539         if (hoid->key_len) {
1540                 *pkey = hoid->key;
1541                 *pkey_len = hoid->key_len;
1542         } else {
1543                 *pkey = hoid->oid;
1544                 *pkey_len = hoid->oid_len;
1545         }
1546 }
1547
1548 static int compare_names(const void *name1, size_t name1_len,
1549                          const void *name2, size_t name2_len)
1550 {
1551         int ret;
1552
1553         ret = memcmp(name1, name2, min(name1_len, name2_len));
1554         if (!ret) {
1555                 if (name1_len < name2_len)
1556                         ret = -1;
1557                 else if (name1_len > name2_len)
1558                         ret = 1;
1559         }
1560         return ret;
1561 }
1562
1563 static int hoid_compare(const struct ceph_hobject_id *lhs,
1564                         const struct ceph_hobject_id *rhs)
1565 {
1566         void *effective_key1, *effective_key2;
1567         size_t effective_key1_len, effective_key2_len;
1568         int ret;
1569
1570         if (lhs->is_max < rhs->is_max)
1571                 return -1;
1572         if (lhs->is_max > rhs->is_max)
1573                 return 1;
1574
1575         if (lhs->pool < rhs->pool)
1576                 return -1;
1577         if (lhs->pool > rhs->pool)
1578                 return 1;
1579
1580         if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1581                 return -1;
1582         if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1583                 return 1;
1584
1585         ret = compare_names(lhs->nspace, lhs->nspace_len,
1586                             rhs->nspace, rhs->nspace_len);
1587         if (ret)
1588                 return ret;
1589
1590         hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1591         hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1592         ret = compare_names(effective_key1, effective_key1_len,
1593                             effective_key2, effective_key2_len);
1594         if (ret)
1595                 return ret;
1596
1597         ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1598         if (ret)
1599                 return ret;
1600
1601         if (lhs->snapid < rhs->snapid)
1602                 return -1;
1603         if (lhs->snapid > rhs->snapid)
1604                 return 1;
1605
1606         return 0;
1607 }
1608
1609 /*
1610  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1611  * compat stuff here.
1612  *
1613  * Assumes @hoid is zero-initialized.
1614  */
1615 static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1616 {
1617         u8 struct_v;
1618         u32 struct_len;
1619         int ret;
1620
1621         ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1622                                   &struct_len);
1623         if (ret)
1624                 return ret;
1625
1626         if (struct_v < 4) {
1627                 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1628                 goto e_inval;
1629         }
1630
1631         hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1632                                                 GFP_NOIO);
1633         if (IS_ERR(hoid->key)) {
1634                 ret = PTR_ERR(hoid->key);
1635                 hoid->key = NULL;
1636                 return ret;
1637         }
1638
1639         hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1640                                                 GFP_NOIO);
1641         if (IS_ERR(hoid->oid)) {
1642                 ret = PTR_ERR(hoid->oid);
1643                 hoid->oid = NULL;
1644                 return ret;
1645         }
1646
1647         ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1648         ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1649         ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1650
1651         hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1652                                                    GFP_NOIO);
1653         if (IS_ERR(hoid->nspace)) {
1654                 ret = PTR_ERR(hoid->nspace);
1655                 hoid->nspace = NULL;
1656                 return ret;
1657         }
1658
1659         ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1660
1661         ceph_hoid_build_hash_cache(hoid);
1662         return 0;
1663
1664 e_inval:
1665         return -EINVAL;
1666 }
1667
1668 static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1669 {
1670         return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1671                4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1672 }
1673
1674 static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1675 {
1676         ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1677         ceph_encode_string(p, end, hoid->key, hoid->key_len);
1678         ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1679         ceph_encode_64(p, hoid->snapid);
1680         ceph_encode_32(p, hoid->hash);
1681         ceph_encode_8(p, hoid->is_max);
1682         ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1683         ceph_encode_64(p, hoid->pool);
1684 }
1685
1686 static void free_hoid(struct ceph_hobject_id *hoid)
1687 {
1688         if (hoid) {
1689                 kfree(hoid->key);
1690                 kfree(hoid->oid);
1691                 kfree(hoid->nspace);
1692                 kfree(hoid);
1693         }
1694 }
1695
1696 static struct ceph_osd_backoff *alloc_backoff(void)
1697 {
1698         struct ceph_osd_backoff *backoff;
1699
1700         backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1701         if (!backoff)
1702                 return NULL;
1703
1704         RB_CLEAR_NODE(&backoff->spg_node);
1705         RB_CLEAR_NODE(&backoff->id_node);
1706         return backoff;
1707 }
1708
1709 static void free_backoff(struct ceph_osd_backoff *backoff)
1710 {
1711         WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1712         WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1713
1714         free_hoid(backoff->begin);
1715         free_hoid(backoff->end);
1716         kfree(backoff);
1717 }
1718
1719 /*
1720  * Within a specific spgid, backoffs are managed by ->begin hoid.
1721  */
1722 DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1723                         RB_BYVAL, spg_node);
1724
1725 static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1726                                             const struct ceph_hobject_id *hoid)
1727 {
1728         struct rb_node *n = root->rb_node;
1729
1730         while (n) {
1731                 struct ceph_osd_backoff *cur =
1732                     rb_entry(n, struct ceph_osd_backoff, spg_node);
1733                 int cmp;
1734
1735                 cmp = hoid_compare(hoid, cur->begin);
1736                 if (cmp < 0) {
1737                         n = n->rb_left;
1738                 } else if (cmp > 0) {
1739                         if (hoid_compare(hoid, cur->end) < 0)
1740                                 return cur;
1741
1742                         n = n->rb_right;
1743                 } else {
1744                         return cur;
1745                 }
1746         }
1747
1748         return NULL;
1749 }
1750
1751 /*
1752  * Each backoff has a unique id within its OSD session.
1753  */
1754 DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1755
1756 static void clear_backoffs(struct ceph_osd *osd)
1757 {
1758         while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1759                 struct ceph_spg_mapping *spg =
1760                     rb_entry(rb_first(&osd->o_backoff_mappings),
1761                              struct ceph_spg_mapping, node);
1762
1763                 while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1764                         struct ceph_osd_backoff *backoff =
1765                             rb_entry(rb_first(&spg->backoffs),
1766                                      struct ceph_osd_backoff, spg_node);
1767
1768                         erase_backoff(&spg->backoffs, backoff);
1769                         erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1770                         free_backoff(backoff);
1771                 }
1772                 erase_spg_mapping(&osd->o_backoff_mappings, spg);
1773                 free_spg_mapping(spg);
1774         }
1775 }
1776
1777 /*
1778  * Set up a temporary, non-owning view into @t.
1779  */
1780 static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1781                                   const struct ceph_osd_request_target *t)
1782 {
1783         hoid->key = NULL;
1784         hoid->key_len = 0;
1785         hoid->oid = t->target_oid.name;
1786         hoid->oid_len = t->target_oid.name_len;
1787         hoid->snapid = CEPH_NOSNAP;
1788         hoid->hash = t->pgid.seed;
1789         hoid->is_max = false;
1790         if (t->target_oloc.pool_ns) {
1791                 hoid->nspace = t->target_oloc.pool_ns->str;
1792                 hoid->nspace_len = t->target_oloc.pool_ns->len;
1793         } else {
1794                 hoid->nspace = NULL;
1795                 hoid->nspace_len = 0;
1796         }
1797         hoid->pool = t->target_oloc.pool;
1798         ceph_hoid_build_hash_cache(hoid);
1799 }
1800
1801 static bool should_plug_request(struct ceph_osd_request *req)
1802 {
1803         struct ceph_osd *osd = req->r_osd;
1804         struct ceph_spg_mapping *spg;
1805         struct ceph_osd_backoff *backoff;
1806         struct ceph_hobject_id hoid;
1807
1808         spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1809         if (!spg)
1810                 return false;
1811
1812         hoid_fill_from_target(&hoid, &req->r_t);
1813         backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1814         if (!backoff)
1815                 return false;
1816
1817         dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1818              __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1819              backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1820         return true;
1821 }
1822
1823 static void setup_request_data(struct ceph_osd_request *req,
1824                                struct ceph_msg *msg)
1825 {
1826         u32 data_len = 0;
1827         int i;
1828
1829         if (!list_empty(&msg->data))
1830                 return;
1831
1832         WARN_ON(msg->data_length);
1833         for (i = 0; i < req->r_num_ops; i++) {
1834                 struct ceph_osd_req_op *op = &req->r_ops[i];
1835
1836                 switch (op->op) {
1837                 /* request */
1838                 case CEPH_OSD_OP_WRITE:
1839                 case CEPH_OSD_OP_WRITEFULL:
1840                         WARN_ON(op->indata_len != op->extent.length);
1841                         ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1842                         break;
1843                 case CEPH_OSD_OP_SETXATTR:
1844                 case CEPH_OSD_OP_CMPXATTR:
1845                         WARN_ON(op->indata_len != op->xattr.name_len +
1846                                                   op->xattr.value_len);
1847                         ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1848                         break;
1849                 case CEPH_OSD_OP_NOTIFY_ACK:
1850                         ceph_osdc_msg_data_add(msg,
1851                                                &op->notify_ack.request_data);
1852                         break;
1853
1854                 /* reply */
1855                 case CEPH_OSD_OP_STAT:
1856                         ceph_osdc_msg_data_add(req->r_reply,
1857                                                &op->raw_data_in);
1858                         break;
1859                 case CEPH_OSD_OP_READ:
1860                         ceph_osdc_msg_data_add(req->r_reply,
1861                                                &op->extent.osd_data);
1862                         break;
1863                 case CEPH_OSD_OP_LIST_WATCHERS:
1864                         ceph_osdc_msg_data_add(req->r_reply,
1865                                                &op->list_watchers.response_data);
1866                         break;
1867
1868                 /* both */
1869                 case CEPH_OSD_OP_CALL:
1870                         WARN_ON(op->indata_len != op->cls.class_len +
1871                                                   op->cls.method_len +
1872                                                   op->cls.indata_len);
1873                         ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1874                         /* optional, can be NONE */
1875                         ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1876                         /* optional, can be NONE */
1877                         ceph_osdc_msg_data_add(req->r_reply,
1878                                                &op->cls.response_data);
1879                         break;
1880                 case CEPH_OSD_OP_NOTIFY:
1881                         ceph_osdc_msg_data_add(msg,
1882                                                &op->notify.request_data);
1883                         ceph_osdc_msg_data_add(req->r_reply,
1884                                                &op->notify.response_data);
1885                         break;
1886                 }
1887
1888                 data_len += op->indata_len;
1889         }
1890
1891         WARN_ON(data_len != msg->data_length);
1892 }
1893
1894 static void encode_pgid(void **p, const struct ceph_pg *pgid)
1895 {
1896         ceph_encode_8(p, 1);
1897         ceph_encode_64(p, pgid->pool);
1898         ceph_encode_32(p, pgid->seed);
1899         ceph_encode_32(p, -1); /* preferred */
1900 }
1901
1902 static void encode_spgid(void **p, const struct ceph_spg *spgid)
1903 {
1904         ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
1905         encode_pgid(p, &spgid->pgid);
1906         ceph_encode_8(p, spgid->shard);
1907 }
1908
1909 static void encode_oloc(void **p, void *end,
1910                         const struct ceph_object_locator *oloc)
1911 {
1912         ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
1913         ceph_encode_64(p, oloc->pool);
1914         ceph_encode_32(p, -1); /* preferred */
1915         ceph_encode_32(p, 0);  /* key len */
1916         if (oloc->pool_ns)
1917                 ceph_encode_string(p, end, oloc->pool_ns->str,
1918                                    oloc->pool_ns->len);
1919         else
1920                 ceph_encode_32(p, 0);
1921 }
1922
1923 static void encode_request_partial(struct ceph_osd_request *req,
1924                                    struct ceph_msg *msg)
1925 {
1926         void *p = msg->front.iov_base;
1927         void *const end = p + msg->front_alloc_len;
1928         u32 data_len = 0;
1929         int i;
1930
1931         if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1932                 /* snapshots aren't writeable */
1933                 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1934         } else {
1935                 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1936                         req->r_data_offset || req->r_snapc);
1937         }
1938
1939         setup_request_data(req, msg);
1940
1941         encode_spgid(&p, &req->r_t.spgid); /* actual spg */
1942         ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
1943         ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1944         ceph_encode_32(&p, req->r_flags);
1945
1946         /* reqid */
1947         ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
1948         memset(p, 0, sizeof(struct ceph_osd_reqid));
1949         p += sizeof(struct ceph_osd_reqid);
1950
1951         /* trace */
1952         memset(p, 0, sizeof(struct ceph_blkin_trace_info));
1953         p += sizeof(struct ceph_blkin_trace_info);
1954
1955         ceph_encode_32(&p, 0); /* client_inc, always 0 */
1956         ceph_encode_timespec(p, &req->r_mtime);
1957         p += sizeof(struct ceph_timespec);
1958
1959         encode_oloc(&p, end, &req->r_t.target_oloc);
1960         ceph_encode_string(&p, end, req->r_t.target_oid.name,
1961                            req->r_t.target_oid.name_len);
1962
1963         /* ops, can imply data */
1964         ceph_encode_16(&p, req->r_num_ops);
1965         for (i = 0; i < req->r_num_ops; i++) {
1966                 data_len += osd_req_encode_op(p, &req->r_ops[i]);
1967                 p += sizeof(struct ceph_osd_op);
1968         }
1969
1970         ceph_encode_64(&p, req->r_snapid); /* snapid */
1971         if (req->r_snapc) {
1972                 ceph_encode_64(&p, req->r_snapc->seq);
1973                 ceph_encode_32(&p, req->r_snapc->num_snaps);
1974                 for (i = 0; i < req->r_snapc->num_snaps; i++)
1975                         ceph_encode_64(&p, req->r_snapc->snaps[i]);
1976         } else {
1977                 ceph_encode_64(&p, 0); /* snap_seq */
1978                 ceph_encode_32(&p, 0); /* snaps len */
1979         }
1980
1981         ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1982         BUG_ON(p > end - 8); /* space for features */
1983
1984         msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
1985         /* front_len is finalized in encode_request_finish() */
1986         msg->front.iov_len = p - msg->front.iov_base;
1987         msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1988         msg->hdr.data_len = cpu_to_le32(data_len);
1989         /*
1990          * The header "data_off" is a hint to the receiver allowing it
1991          * to align received data into its buffers such that there's no
1992          * need to re-copy it before writing it to disk (direct I/O).
1993          */
1994         msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1995
1996         dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
1997              req->r_t.target_oid.name, req->r_t.target_oid.name_len);
1998 }
1999
2000 static void encode_request_finish(struct ceph_msg *msg)
2001 {
2002         void *p = msg->front.iov_base;
2003         void *const partial_end = p + msg->front.iov_len;
2004         void *const end = p + msg->front_alloc_len;
2005
2006         if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
2007                 /* luminous OSD -- encode features and be done */
2008                 p = partial_end;
2009                 ceph_encode_64(&p, msg->con->peer_features);
2010         } else {
2011                 struct {
2012                         char spgid[CEPH_ENCODING_START_BLK_LEN +
2013                                    CEPH_PGID_ENCODING_LEN + 1];
2014                         __le32 hash;
2015                         __le32 epoch;
2016                         __le32 flags;
2017                         char reqid[CEPH_ENCODING_START_BLK_LEN +
2018                                    sizeof(struct ceph_osd_reqid)];
2019                         char trace[sizeof(struct ceph_blkin_trace_info)];
2020                         __le32 client_inc;
2021                         struct ceph_timespec mtime;
2022                 } __packed head;
2023                 struct ceph_pg pgid;
2024                 void *oloc, *oid, *tail;
2025                 int oloc_len, oid_len, tail_len;
2026                 int len;
2027
2028                 /*
2029                  * Pre-luminous OSD -- reencode v8 into v4 using @head
2030                  * as a temporary buffer.  Encode the raw PG; the rest
2031                  * is just a matter of moving oloc, oid and tail blobs
2032                  * around.
2033                  */
2034                 memcpy(&head, p, sizeof(head));
2035                 p += sizeof(head);
2036
2037                 oloc = p;
2038                 p += CEPH_ENCODING_START_BLK_LEN;
2039                 pgid.pool = ceph_decode_64(&p);
2040                 p += 4 + 4; /* preferred, key len */
2041                 len = ceph_decode_32(&p);
2042                 p += len;   /* nspace */
2043                 oloc_len = p - oloc;
2044
2045                 oid = p;
2046                 len = ceph_decode_32(&p);
2047                 p += len;
2048                 oid_len = p - oid;
2049
2050                 tail = p;
2051                 tail_len = partial_end - p;
2052
2053                 p = msg->front.iov_base;
2054                 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
2055                 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
2056                 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
2057                 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
2058
2059                 /* reassert_version */
2060                 memset(p, 0, sizeof(struct ceph_eversion));
2061                 p += sizeof(struct ceph_eversion);
2062
2063                 BUG_ON(p >= oloc);
2064                 memmove(p, oloc, oloc_len);
2065                 p += oloc_len;
2066
2067                 pgid.seed = le32_to_cpu(head.hash);
2068                 encode_pgid(&p, &pgid); /* raw pg */
2069
2070                 BUG_ON(p >= oid);
2071                 memmove(p, oid, oid_len);
2072                 p += oid_len;
2073
2074                 /* tail -- ops, snapid, snapc, retry_attempt */
2075                 BUG_ON(p >= tail);
2076                 memmove(p, tail, tail_len);
2077                 p += tail_len;
2078
2079                 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
2080         }
2081
2082         BUG_ON(p > end);
2083         msg->front.iov_len = p - msg->front.iov_base;
2084         msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2085
2086         dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
2087              le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
2088              le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
2089              le16_to_cpu(msg->hdr.version));
2090 }
2091
2092 /*
2093  * @req has to be assigned a tid and registered.
2094  */
2095 static void send_request(struct ceph_osd_request *req)
2096 {
2097         struct ceph_osd *osd = req->r_osd;
2098
2099         verify_osd_locked(osd);
2100         WARN_ON(osd->o_osd != req->r_t.osd);
2101
2102         /* backoff? */
2103         if (should_plug_request(req))
2104                 return;
2105
2106         /*
2107          * We may have a previously queued request message hanging
2108          * around.  Cancel it to avoid corrupting the msgr.
2109          */
2110         if (req->r_sent)
2111                 ceph_msg_revoke(req->r_request);
2112
2113         req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2114         if (req->r_attempts)
2115                 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2116         else
2117                 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2118
2119         encode_request_partial(req, req->r_request);
2120
2121         dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
2122              __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2123              req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
2124              req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
2125              req->r_attempts);
2126
2127         req->r_t.paused = false;
2128         req->r_stamp = jiffies;
2129         req->r_attempts++;
2130
2131         req->r_sent = osd->o_incarnation;
2132         req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2133         ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
2134 }
2135
2136 static void maybe_request_map(struct ceph_osd_client *osdc)
2137 {
2138         bool continuous = false;
2139
2140         verify_osdc_locked(osdc);
2141         WARN_ON(!osdc->osdmap->epoch);
2142
2143         if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2144             ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2145             ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2146                 dout("%s osdc %p continuous\n", __func__, osdc);
2147                 continuous = true;
2148         } else {
2149                 dout("%s osdc %p onetime\n", __func__, osdc);
2150         }
2151
2152         if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2153                                osdc->osdmap->epoch + 1, continuous))
2154                 ceph_monc_renew_subs(&osdc->client->monc);
2155 }
2156
2157 static void complete_request(struct ceph_osd_request *req, int err);
2158 static void send_map_check(struct ceph_osd_request *req);
2159
2160 static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
2161 {
2162         struct ceph_osd_client *osdc = req->r_osdc;
2163         struct ceph_osd *osd;
2164         enum calc_target_result ct_res;
2165         bool need_send = false;
2166         bool promoted = false;
2167         bool need_abort = false;
2168
2169         WARN_ON(req->r_tid);
2170         dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
2171
2172 again:
2173         ct_res = calc_target(osdc, &req->r_t, NULL, false);
2174         if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
2175                 goto promote;
2176
2177         osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
2178         if (IS_ERR(osd)) {
2179                 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
2180                 goto promote;
2181         }
2182
2183         if (osdc->osdmap->epoch < osdc->epoch_barrier) {
2184                 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
2185                      osdc->epoch_barrier);
2186                 req->r_t.paused = true;
2187                 maybe_request_map(osdc);
2188         } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2189                    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2190                 dout("req %p pausewr\n", req);
2191                 req->r_t.paused = true;
2192                 maybe_request_map(osdc);
2193         } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2194                    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2195                 dout("req %p pauserd\n", req);
2196                 req->r_t.paused = true;
2197                 maybe_request_map(osdc);
2198         } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2199                    !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
2200                                      CEPH_OSD_FLAG_FULL_FORCE)) &&
2201                    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2202                     pool_full(osdc, req->r_t.base_oloc.pool))) {
2203                 dout("req %p full/pool_full\n", req);
2204                 pr_warn_ratelimited("FULL or reached pool quota\n");
2205                 req->r_t.paused = true;
2206                 maybe_request_map(osdc);
2207                 if (req->r_abort_on_full)
2208                         need_abort = true;
2209         } else if (!osd_homeless(osd)) {
2210                 need_send = true;
2211         } else {
2212                 maybe_request_map(osdc);
2213         }
2214
2215         mutex_lock(&osd->lock);
2216         /*
2217          * Assign the tid atomically with send_request() to protect
2218          * multiple writes to the same object from racing with each
2219          * other, resulting in out of order ops on the OSDs.
2220          */
2221         req->r_tid = atomic64_inc_return(&osdc->last_tid);
2222         link_request(osd, req);
2223         if (need_send)
2224                 send_request(req);
2225         else if (need_abort)
2226                 complete_request(req, -ENOSPC);
2227         mutex_unlock(&osd->lock);
2228
2229         if (ct_res == CALC_TARGET_POOL_DNE)
2230                 send_map_check(req);
2231
2232         if (promoted)
2233                 downgrade_write(&osdc->lock);
2234         return;
2235
2236 promote:
2237         up_read(&osdc->lock);
2238         down_write(&osdc->lock);
2239         wrlocked = true;
2240         promoted = true;
2241         goto again;
2242 }
2243
2244 static void account_request(struct ceph_osd_request *req)
2245 {
2246         WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2247         WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
2248
2249         req->r_flags |= CEPH_OSD_FLAG_ONDISK;
2250         atomic_inc(&req->r_osdc->num_requests);
2251
2252         req->r_start_stamp = jiffies;
2253 }
2254
2255 static void submit_request(struct ceph_osd_request *req, bool wrlocked)
2256 {
2257         ceph_osdc_get_request(req);
2258         account_request(req);
2259         __submit_request(req, wrlocked);
2260 }
2261
2262 static void finish_request(struct ceph_osd_request *req)
2263 {
2264         struct ceph_osd_client *osdc = req->r_osdc;
2265
2266         WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
2267         dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2268
2269         if (req->r_osd)
2270                 unlink_request(req->r_osd, req);
2271         atomic_dec(&osdc->num_requests);
2272
2273         /*
2274          * If an OSD has failed or returned and a request has been sent
2275          * twice, it's possible to get a reply and end up here while the
2276          * request message is queued for delivery.  We will ignore the
2277          * reply, so not a big deal, but better to try and catch it.
2278          */
2279         ceph_msg_revoke(req->r_request);
2280         ceph_msg_revoke_incoming(req->r_reply);
2281 }
2282
2283 static void __complete_request(struct ceph_osd_request *req)
2284 {
2285         if (req->r_callback) {
2286                 dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2287                      req->r_tid, req->r_callback, req->r_result);
2288                 req->r_callback(req);
2289         }
2290 }
2291
2292 /*
2293  * This is open-coded in handle_reply().
2294  */
2295 static void complete_request(struct ceph_osd_request *req, int err)
2296 {
2297         dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2298
2299         req->r_result = err;
2300         finish_request(req);
2301         __complete_request(req);
2302         complete_all(&req->r_completion);
2303         ceph_osdc_put_request(req);
2304 }
2305
2306 static void cancel_map_check(struct ceph_osd_request *req)
2307 {
2308         struct ceph_osd_client *osdc = req->r_osdc;
2309         struct ceph_osd_request *lookup_req;
2310
2311         verify_osdc_wrlocked(osdc);
2312
2313         lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2314         if (!lookup_req)
2315                 return;
2316
2317         WARN_ON(lookup_req != req);
2318         erase_request_mc(&osdc->map_checks, req);
2319         ceph_osdc_put_request(req);
2320 }
2321
2322 static void cancel_request(struct ceph_osd_request *req)
2323 {
2324         dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2325
2326         cancel_map_check(req);
2327         finish_request(req);
2328         complete_all(&req->r_completion);
2329         ceph_osdc_put_request(req);
2330 }
2331
2332 static void abort_request(struct ceph_osd_request *req, int err)
2333 {
2334         dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2335
2336         cancel_map_check(req);
2337         complete_request(req, err);
2338 }
2339
2340 static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2341 {
2342         if (likely(eb > osdc->epoch_barrier)) {
2343                 dout("updating epoch_barrier from %u to %u\n",
2344                                 osdc->epoch_barrier, eb);
2345                 osdc->epoch_barrier = eb;
2346                 /* Request map if we're not to the barrier yet */
2347                 if (eb > osdc->osdmap->epoch)
2348                         maybe_request_map(osdc);
2349         }
2350 }
2351
2352 void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2353 {
2354         down_read(&osdc->lock);
2355         if (unlikely(eb > osdc->epoch_barrier)) {
2356                 up_read(&osdc->lock);
2357                 down_write(&osdc->lock);
2358                 update_epoch_barrier(osdc, eb);
2359                 up_write(&osdc->lock);
2360         } else {
2361                 up_read(&osdc->lock);
2362         }
2363 }
2364 EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
2365
2366 /*
2367  * Drop all pending requests that are stalled waiting on a full condition to
2368  * clear, and complete them with ENOSPC as the return code. Set the
2369  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2370  * cancelled.
2371  */
2372 static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2373 {
2374         struct rb_node *n;
2375         bool victims = false;
2376
2377         dout("enter abort_on_full\n");
2378
2379         if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
2380                 goto out;
2381
2382         /* Scan list and see if there is anything to abort */
2383         for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2384                 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2385                 struct rb_node *m;
2386
2387                 m = rb_first(&osd->o_requests);
2388                 while (m) {
2389                         struct ceph_osd_request *req = rb_entry(m,
2390                                         struct ceph_osd_request, r_node);
2391                         m = rb_next(m);
2392
2393                         if (req->r_abort_on_full) {
2394                                 victims = true;
2395                                 break;
2396                         }
2397                 }
2398                 if (victims)
2399                         break;
2400         }
2401
2402         if (!victims)
2403                 goto out;
2404
2405         /*
2406          * Update the barrier to current epoch if it's behind that point,
2407          * since we know we have some calls to be aborted in the tree.
2408          */
2409         update_epoch_barrier(osdc, osdc->osdmap->epoch);
2410
2411         for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2412                 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2413                 struct rb_node *m;
2414
2415                 m = rb_first(&osd->o_requests);
2416                 while (m) {
2417                         struct ceph_osd_request *req = rb_entry(m,
2418                                         struct ceph_osd_request, r_node);
2419                         m = rb_next(m);
2420
2421                         if (req->r_abort_on_full &&
2422                             (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2423                              pool_full(osdc, req->r_t.target_oloc.pool)))
2424                                 abort_request(req, -ENOSPC);
2425                 }
2426         }
2427 out:
2428         dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
2429 }
2430
2431 static void check_pool_dne(struct ceph_osd_request *req)
2432 {
2433         struct ceph_osd_client *osdc = req->r_osdc;
2434         struct ceph_osdmap *map = osdc->osdmap;
2435
2436         verify_osdc_wrlocked(osdc);
2437         WARN_ON(!map->epoch);
2438
2439         if (req->r_attempts) {
2440                 /*
2441                  * We sent a request earlier, which means that
2442                  * previously the pool existed, and now it does not
2443                  * (i.e., it was deleted).
2444                  */
2445                 req->r_map_dne_bound = map->epoch;
2446                 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2447                      req->r_tid);
2448         } else {
2449                 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2450                      req, req->r_tid, req->r_map_dne_bound, map->epoch);
2451         }
2452
2453         if (req->r_map_dne_bound) {
2454                 if (map->epoch >= req->r_map_dne_bound) {
2455                         /* we had a new enough map */
2456                         pr_info_ratelimited("tid %llu pool does not exist\n",
2457                                             req->r_tid);
2458                         complete_request(req, -ENOENT);
2459                 }
2460         } else {
2461                 send_map_check(req);
2462         }
2463 }
2464
2465 static void map_check_cb(struct ceph_mon_generic_request *greq)
2466 {
2467         struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2468         struct ceph_osd_request *req;
2469         u64 tid = greq->private_data;
2470
2471         WARN_ON(greq->result || !greq->u.newest);
2472
2473         down_write(&osdc->lock);
2474         req = lookup_request_mc(&osdc->map_checks, tid);
2475         if (!req) {
2476                 dout("%s tid %llu dne\n", __func__, tid);
2477                 goto out_unlock;
2478         }
2479
2480         dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2481              req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2482         if (!req->r_map_dne_bound)
2483                 req->r_map_dne_bound = greq->u.newest;
2484         erase_request_mc(&osdc->map_checks, req);
2485         check_pool_dne(req);
2486
2487         ceph_osdc_put_request(req);
2488 out_unlock:
2489         up_write(&osdc->lock);
2490 }
2491
2492 static void send_map_check(struct ceph_osd_request *req)
2493 {
2494         struct ceph_osd_client *osdc = req->r_osdc;
2495         struct ceph_osd_request *lookup_req;
2496         int ret;
2497
2498         verify_osdc_wrlocked(osdc);
2499
2500         lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2501         if (lookup_req) {
2502                 WARN_ON(lookup_req != req);
2503                 return;
2504         }
2505
2506         ceph_osdc_get_request(req);
2507         insert_request_mc(&osdc->map_checks, req);
2508         ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2509                                           map_check_cb, req->r_tid);
2510         WARN_ON(ret);
2511 }
2512
2513 /*
2514  * lingering requests, watch/notify v2 infrastructure
2515  */
2516 static void linger_release(struct kref *kref)
2517 {
2518         struct ceph_osd_linger_request *lreq =
2519             container_of(kref, struct ceph_osd_linger_request, kref);
2520
2521         dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2522              lreq->reg_req, lreq->ping_req);
2523         WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2524         WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
2525         WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2526         WARN_ON(!list_empty(&lreq->scan_item));
2527         WARN_ON(!list_empty(&lreq->pending_lworks));
2528         WARN_ON(lreq->osd);
2529
2530         if (lreq->reg_req)
2531                 ceph_osdc_put_request(lreq->reg_req);
2532         if (lreq->ping_req)
2533                 ceph_osdc_put_request(lreq->ping_req);
2534         target_destroy(&lreq->t);
2535         kfree(lreq);
2536 }
2537
2538 static void linger_put(struct ceph_osd_linger_request *lreq)
2539 {
2540         if (lreq)
2541                 kref_put(&lreq->kref, linger_release);
2542 }
2543
2544 static struct ceph_osd_linger_request *
2545 linger_get(struct ceph_osd_linger_request *lreq)
2546 {
2547         kref_get(&lreq->kref);
2548         return lreq;
2549 }
2550
2551 static struct ceph_osd_linger_request *
2552 linger_alloc(struct ceph_osd_client *osdc)
2553 {
2554         struct ceph_osd_linger_request *lreq;
2555
2556         lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2557         if (!lreq)
2558                 return NULL;
2559
2560         kref_init(&lreq->kref);
2561         mutex_init(&lreq->lock);
2562         RB_CLEAR_NODE(&lreq->node);
2563         RB_CLEAR_NODE(&lreq->osdc_node);
2564         RB_CLEAR_NODE(&lreq->mc_node);
2565         INIT_LIST_HEAD(&lreq->scan_item);
2566         INIT_LIST_HEAD(&lreq->pending_lworks);
2567         init_completion(&lreq->reg_commit_wait);
2568         init_completion(&lreq->notify_finish_wait);
2569
2570         lreq->osdc = osdc;
2571         target_init(&lreq->t);
2572
2573         dout("%s lreq %p\n", __func__, lreq);
2574         return lreq;
2575 }
2576
2577 DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2578 DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
2579 DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2580
2581 /*
2582  * Create linger request <-> OSD session relation.
2583  *
2584  * @lreq has to be registered, @osd may be homeless.
2585  */
2586 static void link_linger(struct ceph_osd *osd,
2587                         struct ceph_osd_linger_request *lreq)
2588 {
2589         verify_osd_locked(osd);
2590         WARN_ON(!lreq->linger_id || lreq->osd);
2591         dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2592              osd->o_osd, lreq, lreq->linger_id);
2593
2594         if (!osd_homeless(osd))
2595                 __remove_osd_from_lru(osd);
2596         else
2597                 atomic_inc(&osd->o_osdc->num_homeless);
2598
2599         get_osd(osd);
2600         insert_linger(&osd->o_linger_requests, lreq);
2601         lreq->osd = osd;
2602 }
2603
2604 static void unlink_linger(struct ceph_osd *osd,
2605                           struct ceph_osd_linger_request *lreq)
2606 {
2607         verify_osd_locked(osd);
2608         WARN_ON(lreq->osd != osd);
2609         dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2610              osd->o_osd, lreq, lreq->linger_id);
2611
2612         lreq->osd = NULL;
2613         erase_linger(&osd->o_linger_requests, lreq);
2614         put_osd(osd);
2615
2616         if (!osd_homeless(osd))
2617                 maybe_move_osd_to_lru(osd);
2618         else
2619                 atomic_dec(&osd->o_osdc->num_homeless);
2620 }
2621
2622 static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2623 {
2624         verify_osdc_locked(lreq->osdc);
2625
2626         return !RB_EMPTY_NODE(&lreq->osdc_node);
2627 }
2628
2629 static bool linger_registered(struct ceph_osd_linger_request *lreq)
2630 {
2631         struct ceph_osd_client *osdc = lreq->osdc;
2632         bool registered;
2633
2634         down_read(&osdc->lock);
2635         registered = __linger_registered(lreq);
2636         up_read(&osdc->lock);
2637
2638         return registered;
2639 }
2640
2641 static void linger_register(struct ceph_osd_linger_request *lreq)
2642 {
2643         struct ceph_osd_client *osdc = lreq->osdc;
2644
2645         verify_osdc_wrlocked(osdc);
2646         WARN_ON(lreq->linger_id);
2647
2648         linger_get(lreq);
2649         lreq->linger_id = ++osdc->last_linger_id;
2650         insert_linger_osdc(&osdc->linger_requests, lreq);
2651 }
2652
2653 static void linger_unregister(struct ceph_osd_linger_request *lreq)
2654 {
2655         struct ceph_osd_client *osdc = lreq->osdc;
2656
2657         verify_osdc_wrlocked(osdc);
2658
2659         erase_linger_osdc(&osdc->linger_requests, lreq);
2660         linger_put(lreq);
2661 }
2662
2663 static void cancel_linger_request(struct ceph_osd_request *req)
2664 {
2665         struct ceph_osd_linger_request *lreq = req->r_priv;
2666
2667         WARN_ON(!req->r_linger);
2668         cancel_request(req);
2669         linger_put(lreq);
2670 }
2671
2672 struct linger_work {
2673         struct work_struct work;
2674         struct ceph_osd_linger_request *lreq;
2675         struct list_head pending_item;
2676         unsigned long queued_stamp;
2677
2678         union {
2679                 struct {
2680                         u64 notify_id;
2681                         u64 notifier_id;
2682                         void *payload; /* points into @msg front */
2683                         size_t payload_len;
2684
2685                         struct ceph_msg *msg; /* for ceph_msg_put() */
2686                 } notify;
2687                 struct {
2688                         int err;
2689                 } error;
2690         };
2691 };
2692
2693 static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2694                                        work_func_t workfn)
2695 {
2696         struct linger_work *lwork;
2697
2698         lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2699         if (!lwork)
2700                 return NULL;
2701
2702         INIT_WORK(&lwork->work, workfn);
2703         INIT_LIST_HEAD(&lwork->pending_item);
2704         lwork->lreq = linger_get(lreq);
2705
2706         return lwork;
2707 }
2708
2709 static void lwork_free(struct linger_work *lwork)
2710 {
2711         struct ceph_osd_linger_request *lreq = lwork->lreq;
2712
2713         mutex_lock(&lreq->lock);
2714         list_del(&lwork->pending_item);
2715         mutex_unlock(&lreq->lock);
2716
2717         linger_put(lreq);
2718         kfree(lwork);
2719 }
2720
2721 static void lwork_queue(struct linger_work *lwork)
2722 {
2723         struct ceph_osd_linger_request *lreq = lwork->lreq;
2724         struct ceph_osd_client *osdc = lreq->osdc;
2725
2726         verify_lreq_locked(lreq);
2727         WARN_ON(!list_empty(&lwork->pending_item));
2728
2729         lwork->queued_stamp = jiffies;
2730         list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2731         queue_work(osdc->notify_wq, &lwork->work);
2732 }
2733
2734 static void do_watch_notify(struct work_struct *w)
2735 {
2736         struct linger_work *lwork = container_of(w, struct linger_work, work);
2737         struct ceph_osd_linger_request *lreq = lwork->lreq;
2738
2739         if (!linger_registered(lreq)) {
2740                 dout("%s lreq %p not registered\n", __func__, lreq);
2741                 goto out;
2742         }
2743
2744         WARN_ON(!lreq->is_watch);
2745         dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2746              __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2747              lwork->notify.payload_len);
2748         lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2749                   lwork->notify.notifier_id, lwork->notify.payload,
2750                   lwork->notify.payload_len);
2751
2752 out:
2753         ceph_msg_put(lwork->notify.msg);
2754         lwork_free(lwork);
2755 }
2756
2757 static void do_watch_error(struct work_struct *w)
2758 {
2759         struct linger_work *lwork = container_of(w, struct linger_work, work);
2760         struct ceph_osd_linger_request *lreq = lwork->lreq;
2761
2762         if (!linger_registered(lreq)) {
2763                 dout("%s lreq %p not registered\n", __func__, lreq);
2764                 goto out;
2765         }
2766
2767         dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2768         lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2769
2770 out:
2771         lwork_free(lwork);
2772 }
2773
2774 static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2775 {
2776         struct linger_work *lwork;
2777
2778         lwork = lwork_alloc(lreq, do_watch_error);
2779         if (!lwork) {
2780                 pr_err("failed to allocate error-lwork\n");
2781                 return;
2782         }
2783
2784         lwork->error.err = lreq->last_error;
2785         lwork_queue(lwork);
2786 }
2787
2788 static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2789                                        int result)
2790 {
2791         if (!completion_done(&lreq->reg_commit_wait)) {
2792                 lreq->reg_commit_error = (result <= 0 ? result : 0);
2793                 complete_all(&lreq->reg_commit_wait);
2794         }
2795 }
2796
2797 static void linger_commit_cb(struct ceph_osd_request *req)
2798 {
2799         struct ceph_osd_linger_request *lreq = req->r_priv;
2800
2801         mutex_lock(&lreq->lock);
2802         dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2803              lreq->linger_id, req->r_result);
2804         linger_reg_commit_complete(lreq, req->r_result);
2805         lreq->committed = true;
2806
2807         if (!lreq->is_watch) {
2808                 struct ceph_osd_data *osd_data =
2809                     osd_req_op_data(req, 0, notify, response_data);
2810                 void *p = page_address(osd_data->pages[0]);
2811
2812                 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2813                         osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2814
2815                 /* make note of the notify_id */
2816                 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2817                         lreq->notify_id = ceph_decode_64(&p);
2818                         dout("lreq %p notify_id %llu\n", lreq,
2819                              lreq->notify_id);
2820                 } else {
2821                         dout("lreq %p no notify_id\n", lreq);
2822                 }
2823         }
2824
2825         mutex_unlock(&lreq->lock);
2826         linger_put(lreq);
2827 }
2828
2829 static int normalize_watch_error(int err)
2830 {
2831         /*
2832          * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2833          * notification and a failure to reconnect because we raced with
2834          * the delete appear the same to the user.
2835          */
2836         if (err == -ENOENT)
2837                 err = -ENOTCONN;
2838
2839         return err;
2840 }
2841
2842 static void linger_reconnect_cb(struct ceph_osd_request *req)
2843 {
2844         struct ceph_osd_linger_request *lreq = req->r_priv;
2845
2846         mutex_lock(&lreq->lock);
2847         dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2848              lreq, lreq->linger_id, req->r_result, lreq->last_error);
2849         if (req->r_result < 0) {
2850                 if (!lreq->last_error) {
2851                         lreq->last_error = normalize_watch_error(req->r_result);
2852                         queue_watch_error(lreq);
2853                 }
2854         }
2855
2856         mutex_unlock(&lreq->lock);
2857         linger_put(lreq);
2858 }
2859
2860 static void send_linger(struct ceph_osd_linger_request *lreq)
2861 {
2862         struct ceph_osd_request *req = lreq->reg_req;
2863         struct ceph_osd_req_op *op = &req->r_ops[0];
2864
2865         verify_osdc_wrlocked(req->r_osdc);
2866         dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2867
2868         if (req->r_osd)
2869                 cancel_linger_request(req);
2870
2871         request_reinit(req);
2872         ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2873         ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2874         req->r_flags = lreq->t.flags;
2875         req->r_mtime = lreq->mtime;
2876
2877         mutex_lock(&lreq->lock);
2878         if (lreq->is_watch && lreq->committed) {
2879                 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2880                         op->watch.cookie != lreq->linger_id);
2881                 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2882                 op->watch.gen = ++lreq->register_gen;
2883                 dout("lreq %p reconnect register_gen %u\n", lreq,
2884                      op->watch.gen);
2885                 req->r_callback = linger_reconnect_cb;
2886         } else {
2887                 if (!lreq->is_watch)
2888                         lreq->notify_id = 0;
2889                 else
2890                         WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2891                 dout("lreq %p register\n", lreq);
2892                 req->r_callback = linger_commit_cb;
2893         }
2894         mutex_unlock(&lreq->lock);
2895
2896         req->r_priv = linger_get(lreq);
2897         req->r_linger = true;
2898
2899         submit_request(req, true);
2900 }
2901
2902 static void linger_ping_cb(struct ceph_osd_request *req)
2903 {
2904         struct ceph_osd_linger_request *lreq = req->r_priv;
2905
2906         mutex_lock(&lreq->lock);
2907         dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2908              __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2909              lreq->last_error);
2910         if (lreq->register_gen == req->r_ops[0].watch.gen) {
2911                 if (!req->r_result) {
2912                         lreq->watch_valid_thru = lreq->ping_sent;
2913                 } else if (!lreq->last_error) {
2914                         lreq->last_error = normalize_watch_error(req->r_result);
2915                         queue_watch_error(lreq);
2916                 }
2917         } else {
2918                 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2919                      lreq->register_gen, req->r_ops[0].watch.gen);
2920         }
2921
2922         mutex_unlock(&lreq->lock);
2923         linger_put(lreq);
2924 }
2925
2926 static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2927 {
2928         struct ceph_osd_client *osdc = lreq->osdc;
2929         struct ceph_osd_request *req = lreq->ping_req;
2930         struct ceph_osd_req_op *op = &req->r_ops[0];
2931
2932         if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2933                 dout("%s PAUSERD\n", __func__);
2934                 return;
2935         }
2936
2937         lreq->ping_sent = jiffies;
2938         dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2939              __func__, lreq, lreq->linger_id, lreq->ping_sent,
2940              lreq->register_gen);
2941
2942         if (req->r_osd)
2943                 cancel_linger_request(req);
2944
2945         request_reinit(req);
2946         target_copy(&req->r_t, &lreq->t);
2947
2948         WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2949                 op->watch.cookie != lreq->linger_id ||
2950                 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2951         op->watch.gen = lreq->register_gen;
2952         req->r_callback = linger_ping_cb;
2953         req->r_priv = linger_get(lreq);
2954         req->r_linger = true;
2955
2956         ceph_osdc_get_request(req);
2957         account_request(req);
2958         req->r_tid = atomic64_inc_return(&osdc->last_tid);
2959         link_request(lreq->osd, req);
2960         send_request(req);
2961 }
2962
2963 static void linger_submit(struct ceph_osd_linger_request *lreq)
2964 {
2965         struct ceph_osd_client *osdc = lreq->osdc;
2966         struct ceph_osd *osd;
2967
2968         calc_target(osdc, &lreq->t, NULL, false);
2969         osd = lookup_create_osd(osdc, lreq->t.osd, true);
2970         link_linger(osd, lreq);
2971
2972         send_linger(lreq);
2973 }
2974
2975 static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
2976 {
2977         struct ceph_osd_client *osdc = lreq->osdc;
2978         struct ceph_osd_linger_request *lookup_lreq;
2979
2980         verify_osdc_wrlocked(osdc);
2981
2982         lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2983                                        lreq->linger_id);
2984         if (!lookup_lreq)
2985                 return;
2986
2987         WARN_ON(lookup_lreq != lreq);
2988         erase_linger_mc(&osdc->linger_map_checks, lreq);
2989         linger_put(lreq);
2990 }
2991
2992 /*
2993  * @lreq has to be both registered and linked.
2994  */
2995 static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2996 {
2997         if (lreq->is_watch && lreq->ping_req->r_osd)
2998                 cancel_linger_request(lreq->ping_req);
2999         if (lreq->reg_req->r_osd)
3000                 cancel_linger_request(lreq->reg_req);
3001         cancel_linger_map_check(lreq);
3002         unlink_linger(lreq->osd, lreq);
3003         linger_unregister(lreq);
3004 }
3005
3006 static void linger_cancel(struct ceph_osd_linger_request *lreq)
3007 {
3008         struct ceph_osd_client *osdc = lreq->osdc;
3009
3010         down_write(&osdc->lock);
3011         if (__linger_registered(lreq))
3012                 __linger_cancel(lreq);
3013         up_write(&osdc->lock);
3014 }
3015
3016 static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
3017
3018 static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
3019 {
3020         struct ceph_osd_client *osdc = lreq->osdc;
3021         struct ceph_osdmap *map = osdc->osdmap;
3022
3023         verify_osdc_wrlocked(osdc);
3024         WARN_ON(!map->epoch);
3025
3026         if (lreq->register_gen) {
3027                 lreq->map_dne_bound = map->epoch;
3028                 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
3029                      lreq, lreq->linger_id);
3030         } else {
3031                 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
3032                      __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3033                      map->epoch);
3034         }
3035
3036         if (lreq->map_dne_bound) {
3037                 if (map->epoch >= lreq->map_dne_bound) {
3038                         /* we had a new enough map */
3039                         pr_info("linger_id %llu pool does not exist\n",
3040                                 lreq->linger_id);
3041                         linger_reg_commit_complete(lreq, -ENOENT);
3042                         __linger_cancel(lreq);
3043                 }
3044         } else {
3045                 send_linger_map_check(lreq);
3046         }
3047 }
3048
3049 static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
3050 {
3051         struct ceph_osd_client *osdc = &greq->monc->client->osdc;
3052         struct ceph_osd_linger_request *lreq;
3053         u64 linger_id = greq->private_data;
3054
3055         WARN_ON(greq->result || !greq->u.newest);
3056
3057         down_write(&osdc->lock);
3058         lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
3059         if (!lreq) {
3060                 dout("%s linger_id %llu dne\n", __func__, linger_id);
3061                 goto out_unlock;
3062         }
3063
3064         dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3065              __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3066              greq->u.newest);
3067         if (!lreq->map_dne_bound)
3068                 lreq->map_dne_bound = greq->u.newest;
3069         erase_linger_mc(&osdc->linger_map_checks, lreq);
3070         check_linger_pool_dne(lreq);
3071
3072         linger_put(lreq);
3073 out_unlock:
3074         up_write(&osdc->lock);
3075 }
3076
3077 static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3078 {
3079         struct ceph_osd_client *osdc = lreq->osdc;
3080         struct ceph_osd_linger_request *lookup_lreq;
3081         int ret;
3082
3083         verify_osdc_wrlocked(osdc);
3084
3085         lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3086                                        lreq->linger_id);
3087         if (lookup_lreq) {
3088                 WARN_ON(lookup_lreq != lreq);
3089                 return;
3090         }
3091
3092         linger_get(lreq);
3093         insert_linger_mc(&osdc->linger_map_checks, lreq);
3094         ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3095                                           linger_map_check_cb, lreq->linger_id);
3096         WARN_ON(ret);
3097 }
3098
3099 static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3100 {
3101         int ret;
3102
3103         dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3104         ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3105         return ret ?: lreq->reg_commit_error;
3106 }
3107
3108 static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
3109 {
3110         int ret;
3111
3112         dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3113         ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
3114         return ret ?: lreq->notify_finish_error;
3115 }
3116
3117 /*
3118  * Timeout callback, called every N seconds.  When 1 or more OSD
3119  * requests has been active for more than N seconds, we send a keepalive
3120  * (tag + timestamp) to its OSD to ensure any communications channel
3121  * reset is detected.
3122  */
3123 static void handle_timeout(struct work_struct *work)
3124 {
3125         struct ceph_osd_client *osdc =
3126                 container_of(work, struct ceph_osd_client, timeout_work.work);
3127         struct ceph_options *opts = osdc->client->options;
3128         unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
3129         unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
3130         LIST_HEAD(slow_osds);
3131         struct rb_node *n, *p;
3132
3133         dout("%s osdc %p\n", __func__, osdc);
3134         down_write(&osdc->lock);
3135
3136         /*
3137          * ping osds that are a bit slow.  this ensures that if there
3138          * is a break in the TCP connection we will notice, and reopen
3139          * a connection with that osd (from the fault callback).
3140          */
3141         for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3142                 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3143                 bool found = false;
3144
3145                 for (p = rb_first(&osd->o_requests); p; ) {
3146                         struct ceph_osd_request *req =
3147                             rb_entry(p, struct ceph_osd_request, r_node);
3148
3149                         p = rb_next(p); /* abort_request() */
3150
3151                         if (time_before(req->r_stamp, cutoff)) {
3152                                 dout(" req %p tid %llu on osd%d is laggy\n",
3153                                      req, req->r_tid, osd->o_osd);
3154                                 found = true;
3155                         }
3156                         if (opts->osd_request_timeout &&
3157                             time_before(req->r_start_stamp, expiry_cutoff)) {
3158                                 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3159                                        req->r_tid, osd->o_osd);
3160                                 abort_request(req, -ETIMEDOUT);
3161                         }
3162                 }
3163                 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3164                         struct ceph_osd_linger_request *lreq =
3165                             rb_entry(p, struct ceph_osd_linger_request, node);
3166
3167                         dout(" lreq %p linger_id %llu is served by osd%d\n",
3168                              lreq, lreq->linger_id, osd->o_osd);
3169                         found = true;
3170
3171                         mutex_lock(&lreq->lock);
3172                         if (lreq->is_watch && lreq->committed && !lreq->last_error)
3173                                 send_linger_ping(lreq);
3174                         mutex_unlock(&lreq->lock);
3175                 }
3176
3177                 if (found)
3178                         list_move_tail(&osd->o_keepalive_item, &slow_osds);
3179         }
3180
3181         if (opts->osd_request_timeout) {
3182                 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3183                         struct ceph_osd_request *req =
3184                             rb_entry(p, struct ceph_osd_request, r_node);
3185
3186                         p = rb_next(p); /* abort_request() */
3187
3188                         if (time_before(req->r_start_stamp, expiry_cutoff)) {
3189                                 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3190                                        req->r_tid, osdc->homeless_osd.o_osd);
3191                                 abort_request(req, -ETIMEDOUT);
3192                         }
3193                 }
3194         }
3195
3196         if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3197                 maybe_request_map(osdc);
3198
3199         while (!list_empty(&slow_osds)) {
3200                 struct ceph_osd *osd = list_first_entry(&slow_osds,
3201                                                         struct ceph_osd,
3202                                                         o_keepalive_item);
3203                 list_del_init(&osd->o_keepalive_item);
3204                 ceph_con_keepalive(&osd->o_con);
3205         }
3206
3207         up_write(&osdc->lock);
3208         schedule_delayed_work(&osdc->timeout_work,
3209                               osdc->client->options->osd_keepalive_timeout);
3210 }
3211
3212 static void handle_osds_timeout(struct work_struct *work)
3213 {
3214         struct ceph_osd_client *osdc =
3215                 container_of(work, struct ceph_osd_client,
3216                              osds_timeout_work.work);
3217         unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
3218         struct ceph_osd *osd, *nosd;
3219
3220         dout("%s osdc %p\n", __func__, osdc);
3221         down_write(&osdc->lock);
3222         list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3223                 if (time_before(jiffies, osd->lru_ttl))
3224                         break;
3225
3226                 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3227                 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
3228                 close_osd(osd);
3229         }
3230
3231         up_write(&osdc->lock);
3232         schedule_delayed_work(&osdc->osds_timeout_work,
3233                               round_jiffies_relative(delay));
3234 }
3235
3236 static int ceph_oloc_decode(void **p, void *end,
3237                             struct ceph_object_locator *oloc)
3238 {
3239         u8 struct_v, struct_cv;
3240         u32 len;
3241         void *struct_end;
3242         int ret = 0;
3243
3244         ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3245         struct_v = ceph_decode_8(p);
3246         struct_cv = ceph_decode_8(p);
3247         if (struct_v < 3) {
3248                 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3249                         struct_v, struct_cv);
3250                 goto e_inval;
3251         }
3252         if (struct_cv > 6) {
3253                 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3254                         struct_v, struct_cv);
3255                 goto e_inval;
3256         }
3257         len = ceph_decode_32(p);
3258         ceph_decode_need(p, end, len, e_inval);
3259         struct_end = *p + len;
3260
3261         oloc->pool = ceph_decode_64(p);
3262         *p += 4; /* skip preferred */
3263
3264         len = ceph_decode_32(p);
3265         if (len > 0) {
3266                 pr_warn("ceph_object_locator::key is set\n");
3267                 goto e_inval;
3268         }
3269
3270         if (struct_v >= 5) {
3271                 bool changed = false;
3272
3273                 len = ceph_decode_32(p);
3274                 if (len > 0) {
3275                         ceph_decode_need(p, end, len, e_inval);
3276                         if (!oloc->pool_ns ||
3277                             ceph_compare_string(oloc->pool_ns, *p, len))
3278                                 changed = true;
3279                         *p += len;
3280                 } else {
3281                         if (oloc->pool_ns)
3282                                 changed = true;
3283                 }
3284                 if (changed) {
3285                         /* redirect changes namespace */
3286                         pr_warn("ceph_object_locator::nspace is changed\n");
3287                         goto e_inval;
3288                 }
3289         }
3290
3291         if (struct_v >= 6) {
3292                 s64 hash = ceph_decode_64(p);
3293                 if (hash != -1) {
3294                         pr_warn("ceph_object_locator::hash is set\n");
3295                         goto e_inval;
3296                 }
3297         }
3298
3299         /* skip the rest */
3300         *p = struct_end;
3301 out:
3302         return ret;
3303
3304 e_inval:
3305         ret = -EINVAL;
3306         goto out;
3307 }
3308
3309 static int ceph_redirect_decode(void **p, void *end,
3310                                 struct ceph_request_redirect *redir)
3311 {
3312         u8 struct_v, struct_cv;
3313         u32 len;
3314         void *struct_end;
3315         int ret;
3316
3317         ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3318         struct_v = ceph_decode_8(p);
3319         struct_cv = ceph_decode_8(p);
3320         if (struct_cv > 1) {
3321                 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3322                         struct_v, struct_cv);
3323                 goto e_inval;
3324         }
3325         len = ceph_decode_32(p);
3326         ceph_decode_need(p, end, len, e_inval);
3327         struct_end = *p + len;
3328
3329         ret = ceph_oloc_decode(p, end, &redir->oloc);
3330         if (ret)
3331                 goto out;
3332
3333         len = ceph_decode_32(p);
3334         if (len > 0) {
3335                 pr_warn("ceph_request_redirect::object_name is set\n");
3336                 goto e_inval;
3337         }
3338
3339         len = ceph_decode_32(p);
3340         *p += len; /* skip osd_instructions */
3341
3342         /* skip the rest */
3343         *p = struct_end;
3344 out:
3345         return ret;
3346
3347 e_inval:
3348         ret = -EINVAL;
3349         goto out;
3350 }
3351
3352 struct MOSDOpReply {
3353         struct ceph_pg pgid;
3354         u64 flags;
3355         int result;
3356         u32 epoch;
3357         int num_ops;
3358         u32 outdata_len[CEPH_OSD_MAX_OPS];
3359         s32 rval[CEPH_OSD_MAX_OPS];
3360         int retry_attempt;
3361         struct ceph_eversion replay_version;
3362         u64 user_version;
3363         struct ceph_request_redirect redirect;
3364 };
3365
3366 static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
3367 {
3368         void *p = msg->front.iov_base;
3369         void *const end = p + msg->front.iov_len;
3370         u16 version = le16_to_cpu(msg->hdr.version);
3371         struct ceph_eversion bad_replay_version;
3372         u8 decode_redir;
3373         u32 len;
3374         int ret;
3375         int i;
3376
3377         ceph_decode_32_safe(&p, end, len, e_inval);
3378         ceph_decode_need(&p, end, len, e_inval);
3379         p += len; /* skip oid */
3380
3381         ret = ceph_decode_pgid(&p, end, &m->pgid);
3382         if (ret)
3383                 return ret;
3384
3385         ceph_decode_64_safe(&p, end, m->flags, e_inval);
3386         ceph_decode_32_safe(&p, end, m->result, e_inval);
3387         ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3388         memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3389         p += sizeof(bad_replay_version);
3390         ceph_decode_32_safe(&p, end, m->epoch, e_inval);
3391
3392         ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3393         if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3394                 goto e_inval;
3395
3396         ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3397                          e_inval);
3398         for (i = 0; i < m->num_ops; i++) {
3399                 struct ceph_osd_op *op = p;
3400
3401                 m->outdata_len[i] = le32_to_cpu(op->payload_len);
3402                 p += sizeof(*op);
3403         }
3404
3405         ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3406         for (i = 0; i < m->num_ops; i++)
3407                 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3408
3409         if (version >= 5) {
3410                 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3411                 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3412                 p += sizeof(m->replay_version);
3413                 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3414         } else {
3415                 m->replay_version = bad_replay_version; /* struct */
3416                 m->user_version = le64_to_cpu(m->replay_version.version);
3417         }
3418
3419         if (version >= 6) {
3420                 if (version >= 7)
3421                         ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3422                 else
3423                         decode_redir = 1;
3424         } else {
3425                 decode_redir = 0;
3426         }
3427
3428         if (decode_redir) {
3429                 ret = ceph_redirect_decode(&p, end, &m->redirect);
3430                 if (ret)
3431                         return ret;
3432         } else {
3433                 ceph_oloc_init(&m->redirect.oloc);
3434         }
3435
3436         return 0;
3437
3438 e_inval:
3439         return -EINVAL;
3440 }
3441
3442 /*
3443  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3444  * specified.
3445  */
3446 static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3447 {
3448         struct ceph_osd_client *osdc = osd->o_osdc;
3449         struct ceph_osd_request *req;
3450         struct MOSDOpReply m;
3451         u64 tid = le64_to_cpu(msg->hdr.tid);
3452         u32 data_len = 0;
3453         int ret;
3454         int i;
3455
3456         dout("%s msg %p tid %llu\n", __func__, msg, tid);
3457
3458         down_read(&osdc->lock);
3459         if (!osd_registered(osd)) {
3460                 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3461                 goto out_unlock_osdc;
3462         }
3463         WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3464
3465         mutex_lock(&osd->lock);
3466         req = lookup_request(&osd->o_requests, tid);
3467         if (!req) {
3468                 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3469                 goto out_unlock_session;
3470         }
3471
3472         m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3473         ret = decode_MOSDOpReply(msg, &m);
3474         m.redirect.oloc.pool_ns = NULL;
3475         if (ret) {
3476                 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3477                        req->r_tid, ret);
3478                 ceph_msg_dump(msg);
3479                 goto fail_request;
3480         }
3481         dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3482              __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3483              m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3484              le64_to_cpu(m.replay_version.version), m.user_version);
3485
3486         if (m.retry_attempt >= 0) {
3487                 if (m.retry_attempt != req->r_attempts - 1) {
3488                         dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3489                              req, req->r_tid, m.retry_attempt,
3490                              req->r_attempts - 1);
3491                         goto out_unlock_session;
3492                 }
3493         } else {
3494                 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3495         }
3496
3497         if (!ceph_oloc_empty(&m.redirect.oloc)) {
3498                 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3499                      m.redirect.oloc.pool);
3500                 unlink_request(osd, req);
3501                 mutex_unlock(&osd->lock);
3502
3503                 /*
3504                  * Not ceph_oloc_copy() - changing pool_ns is not
3505                  * supported.
3506                  */
3507                 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
3508                 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
3509                 req->r_tid = 0;
3510                 __submit_request(req, false);
3511                 goto out_unlock_osdc;
3512         }
3513
3514         if (m.num_ops != req->r_num_ops) {
3515                 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3516                        req->r_num_ops, req->r_tid);
3517                 goto fail_request;
3518         }
3519         for (i = 0; i < req->r_num_ops; i++) {
3520                 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3521                      req->r_tid, i, m.rval[i], m.outdata_len[i]);
3522                 req->r_ops[i].rval = m.rval[i];
3523                 req->r_ops[i].outdata_len = m.outdata_len[i];
3524                 data_len += m.outdata_len[i];
3525         }
3526         if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3527                 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3528                        le32_to_cpu(msg->hdr.data_len), req->r_tid);
3529                 goto fail_request;
3530         }
3531         dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3532              req, req->r_tid, m.result, data_len);
3533
3534         /*
3535          * Since we only ever request ONDISK, we should only ever get
3536          * one (type of) reply back.
3537          */
3538         WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3539         req->r_result = m.result ?: data_len;
3540         finish_request(req);
3541         mutex_unlock(&osd->lock);
3542         up_read(&osdc->lock);
3543
3544         __complete_request(req);
3545         complete_all(&req->r_completion);
3546         ceph_osdc_put_request(req);
3547         return;
3548
3549 fail_request:
3550         complete_request(req, -EIO);
3551 out_unlock_session:
3552         mutex_unlock(&osd->lock);
3553 out_unlock_osdc:
3554         up_read(&osdc->lock);
3555 }
3556
3557 static void set_pool_was_full(struct ceph_osd_client *osdc)
3558 {
3559         struct rb_node *n;
3560
3561         for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3562                 struct ceph_pg_pool_info *pi =
3563                     rb_entry(n, struct ceph_pg_pool_info, node);
3564
3565                 pi->was_full = __pool_full(pi);
3566         }
3567 }
3568
3569 static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
3570 {
3571         struct ceph_pg_pool_info *pi;
3572
3573         pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3574         if (!pi)
3575                 return false;
3576
3577         return pi->was_full && !__pool_full(pi);
3578 }
3579
3580 static enum calc_target_result
3581 recalc_linger_target(struct ceph_osd_linger_request *lreq)
3582 {
3583         struct ceph_osd_client *osdc = lreq->osdc;
3584         enum calc_target_result ct_res;
3585
3586         ct_res = calc_target(osdc, &lreq->t, NULL, true);
3587         if (ct_res == CALC_TARGET_NEED_RESEND) {
3588                 struct ceph_osd *osd;
3589
3590                 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3591                 if (osd != lreq->osd) {
3592                         unlink_linger(lreq->osd, lreq);
3593                         link_linger(osd, lreq);
3594                 }
3595         }
3596
3597         return ct_res;
3598 }
3599
3600 /*
3601  * Requeue requests whose mapping to an OSD has changed.
3602  */
3603 static void scan_requests(struct ceph_osd *osd,
3604                           bool force_resend,
3605                           bool cleared_full,
3606                           bool check_pool_cleared_full,
3607                           struct rb_root *need_resend,
3608                           struct list_head *need_resend_linger)
3609 {
3610         struct ceph_osd_client *osdc = osd->o_osdc;
3611         struct rb_node *n;
3612         bool force_resend_writes;
3613
3614         for (n = rb_first(&osd->o_linger_requests); n; ) {
3615                 struct ceph_osd_linger_request *lreq =
3616                     rb_entry(n, struct ceph_osd_linger_request, node);
3617                 enum calc_target_result ct_res;
3618
3619                 n = rb_next(n); /* recalc_linger_target() */
3620
3621                 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3622                      lreq->linger_id);
3623                 ct_res = recalc_linger_target(lreq);
3624                 switch (ct_res) {
3625                 case CALC_TARGET_NO_ACTION:
3626                         force_resend_writes = cleared_full ||
3627                             (check_pool_cleared_full &&
3628                              pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3629                         if (!force_resend && !force_resend_writes)
3630                                 break;
3631
3632                         /* fall through */
3633                 case CALC_TARGET_NEED_RESEND:
3634                         cancel_linger_map_check(lreq);
3635                         /*
3636                          * scan_requests() for the previous epoch(s)
3637                          * may have already added it to the list, since
3638                          * it's not unlinked here.
3639                          */
3640                         if (list_empty(&lreq->scan_item))
3641                                 list_add_tail(&lreq->scan_item, need_resend_linger);
3642                         break;
3643                 case CALC_TARGET_POOL_DNE:
3644                         list_del_init(&lreq->scan_item);
3645                         check_linger_pool_dne(lreq);
3646                         break;
3647                 }
3648         }
3649
3650         for (n = rb_first(&osd->o_requests); n; ) {
3651                 struct ceph_osd_request *req =
3652                     rb_entry(n, struct ceph_osd_request, r_node);
3653                 enum calc_target_result ct_res;
3654
3655                 n = rb_next(n); /* unlink_request(), check_pool_dne() */
3656
3657                 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3658                 ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
3659                                      false);
3660                 switch (ct_res) {
3661                 case CALC_TARGET_NO_ACTION:
3662                         force_resend_writes = cleared_full ||
3663                             (check_pool_cleared_full &&
3664                              pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3665                         if (!force_resend &&
3666                             (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3667                              !force_resend_writes))
3668                                 break;
3669
3670                         /* fall through */
3671                 case CALC_TARGET_NEED_RESEND:
3672                         cancel_map_check(req);
3673                         unlink_request(osd, req);
3674                         insert_request(need_resend, req);
3675                         break;
3676                 case CALC_TARGET_POOL_DNE:
3677                         check_pool_dne(req);
3678                         break;
3679                 }
3680         }
3681 }
3682
3683 static int handle_one_map(struct ceph_osd_client *osdc,
3684                           void *p, void *end, bool incremental,
3685                           struct rb_root *need_resend,
3686                           struct list_head *need_resend_linger)
3687 {
3688         struct ceph_osdmap *newmap;
3689         struct rb_node *n;
3690         bool skipped_map = false;
3691         bool was_full;
3692
3693         was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3694         set_pool_was_full(osdc);
3695
3696         if (incremental)
3697                 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3698         else
3699                 newmap = ceph_osdmap_decode(&p, end);
3700         if (IS_ERR(newmap))
3701                 return PTR_ERR(newmap);
3702
3703         if (newmap != osdc->osdmap) {
3704                 /*
3705                  * Preserve ->was_full before destroying the old map.
3706                  * For pools that weren't in the old map, ->was_full
3707                  * should be false.
3708                  */
3709                 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3710                         struct ceph_pg_pool_info *pi =
3711                             rb_entry(n, struct ceph_pg_pool_info, node);
3712                         struct ceph_pg_pool_info *old_pi;
3713
3714                         old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3715                         if (old_pi)
3716                                 pi->was_full = old_pi->was_full;
3717                         else
3718                                 WARN_ON(pi->was_full);
3719                 }
3720
3721                 if (osdc->osdmap->epoch &&
3722                     osdc->osdmap->epoch + 1 < newmap->epoch) {
3723                         WARN_ON(incremental);
3724                         skipped_map = true;
3725                 }
3726
3727                 ceph_osdmap_destroy(osdc->osdmap);
3728                 osdc->osdmap = newmap;
3729         }
3730
3731         was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3732         scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3733                       need_resend, need_resend_linger);
3734
3735         for (n = rb_first(&osdc->osds); n; ) {
3736                 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3737
3738                 n = rb_next(n); /* close_osd() */
3739
3740                 scan_requests(osd, skipped_map, was_full, true, need_resend,
3741                               need_resend_linger);
3742                 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3743                     memcmp(&osd->o_con.peer_addr,
3744                            ceph_osd_addr(osdc->osdmap, osd->o_osd),
3745                            sizeof(struct ceph_entity_addr)))
3746                         close_osd(osd);
3747         }
3748
3749         return 0;
3750 }
3751
3752 static void kick_requests(struct ceph_osd_client *osdc,
3753                           struct rb_root *need_resend,
3754                           struct list_head *need_resend_linger)
3755 {
3756         struct ceph_osd_linger_request *lreq, *nlreq;
3757         enum calc_target_result ct_res;
3758         struct rb_node *n;
3759
3760         /* make sure need_resend targets reflect latest map */
3761         for (n = rb_first(need_resend); n; ) {
3762                 struct ceph_osd_request *req =
3763                     rb_entry(n, struct ceph_osd_request, r_node);
3764
3765                 n = rb_next(n);
3766
3767                 if (req->r_t.epoch < osdc->osdmap->epoch) {
3768                         ct_res = calc_target(osdc, &req->r_t, NULL, false);
3769                         if (ct_res == CALC_TARGET_POOL_DNE) {
3770                                 erase_request(need_resend, req);
3771                                 check_pool_dne(req);
3772                         }
3773                 }
3774         }
3775
3776         for (n = rb_first(need_resend); n; ) {
3777                 struct ceph_osd_request *req =
3778                     rb_entry(n, struct ceph_osd_request, r_node);
3779                 struct ceph_osd *osd;
3780
3781                 n = rb_next(n);
3782                 erase_request(need_resend, req); /* before link_request() */
3783
3784                 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3785                 link_request(osd, req);
3786                 if (!req->r_linger) {
3787                         if (!osd_homeless(osd) && !req->r_t.paused)
3788                                 send_request(req);
3789                 } else {
3790                         cancel_linger_request(req);
3791                 }
3792         }
3793
3794         list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3795                 if (!osd_homeless(lreq->osd))
3796                         send_linger(lreq);
3797
3798                 list_del_init(&lreq->scan_item);
3799         }
3800 }
3801
3802 /*
3803  * Process updated osd map.
3804  *
3805  * The message contains any number of incremental and full maps, normally
3806  * indicating some sort of topology change in the cluster.  Kick requests
3807  * off to different OSDs as needed.
3808  */
3809 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3810 {
3811         void *p = msg->front.iov_base;
3812         void *const end = p + msg->front.iov_len;
3813         u32 nr_maps, maplen;
3814         u32 epoch;
3815         struct ceph_fsid fsid;
3816         struct rb_root need_resend = RB_ROOT;
3817         LIST_HEAD(need_resend_linger);
3818         bool handled_incremental = false;
3819         bool was_pauserd, was_pausewr;
3820         bool pauserd, pausewr;
3821         int err;
3822
3823         dout("%s have %u\n", __func__, osdc->osdmap->epoch);
3824   &