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