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