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