Merge tag 'platform-drivers-x86-v4.17-1' of git://git.infradead.org/linux-platform...
[sfrench/cifs-2.6.git] / net / ceph / osd_client.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/ceph/ceph_debug.h>
4
5 #include <linux/module.h>
6 #include <linux/err.h>
7 #include <linux/highmem.h>
8 #include <linux/mm.h>
9 #include <linux/pagemap.h>
10 #include <linux/slab.h>
11 #include <linux/uaccess.h>
12 #ifdef CONFIG_BLOCK
13 #include <linux/bio.h>
14 #endif
15
16 #include <linux/ceph/ceph_features.h>
17 #include <linux/ceph/libceph.h>
18 #include <linux/ceph/osd_client.h>
19 #include <linux/ceph/messenger.h>
20 #include <linux/ceph/decode.h>
21 #include <linux/ceph/auth.h>
22 #include <linux/ceph/pagelist.h>
23
24 #define OSD_OPREPLY_FRONT_LEN   512
25
26 static struct kmem_cache        *ceph_osd_request_cache;
27
28 static const struct ceph_connection_operations osd_con_ops;
29
30 /*
31  * Implement client access to distributed object storage cluster.
32  *
33  * All data objects are stored within a cluster/cloud of OSDs, or
34  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
35  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
36  * remote daemons serving up and coordinating consistent and safe
37  * access to storage.
38  *
39  * Cluster membership and the mapping of data objects onto storage devices
40  * are described by the osd map.
41  *
42  * We keep track of pending OSD requests (read, write), resubmit
43  * requests to different OSDs when the cluster topology/data layout
44  * change, or retry the affected requests when the communications
45  * channel with an OSD is reset.
46  */
47
48 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
49 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
50 static void link_linger(struct ceph_osd *osd,
51                         struct ceph_osd_linger_request *lreq);
52 static void unlink_linger(struct ceph_osd *osd,
53                           struct ceph_osd_linger_request *lreq);
54 static void clear_backoffs(struct ceph_osd *osd);
55
56 #if 1
57 static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
58 {
59         bool wrlocked = true;
60
61         if (unlikely(down_read_trylock(sem))) {
62                 wrlocked = false;
63                 up_read(sem);
64         }
65
66         return wrlocked;
67 }
68 static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
69 {
70         WARN_ON(!rwsem_is_locked(&osdc->lock));
71 }
72 static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
73 {
74         WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
75 }
76 static inline void verify_osd_locked(struct ceph_osd *osd)
77 {
78         struct ceph_osd_client *osdc = osd->o_osdc;
79
80         WARN_ON(!(mutex_is_locked(&osd->lock) &&
81                   rwsem_is_locked(&osdc->lock)) &&
82                 !rwsem_is_wrlocked(&osdc->lock));
83 }
84 static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
85 {
86         WARN_ON(!mutex_is_locked(&lreq->lock));
87 }
88 #else
89 static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
90 static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
91 static inline void verify_osd_locked(struct ceph_osd *osd) { }
92 static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
93 #endif
94
95 /*
96  * calculate the mapping of a file extent onto an object, and fill out the
97  * request accordingly.  shorten extent as necessary if it crosses an
98  * object boundary.
99  *
100  * fill osd op in request message.
101  */
102 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
103                         u64 *objnum, u64 *objoff, u64 *objlen)
104 {
105         u64 orig_len = *plen;
106         int r;
107
108         /* object extent? */
109         r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
110                                           objoff, objlen);
111         if (r < 0)
112                 return r;
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
121         return 0;
122 }
123
124 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
125 {
126         memset(osd_data, 0, sizeof (*osd_data));
127         osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
128 }
129
130 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
131                         struct page **pages, u64 length, u32 alignment,
132                         bool pages_from_pool, bool own_pages)
133 {
134         osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
135         osd_data->pages = pages;
136         osd_data->length = length;
137         osd_data->alignment = alignment;
138         osd_data->pages_from_pool = pages_from_pool;
139         osd_data->own_pages = own_pages;
140 }
141
142 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
143                         struct ceph_pagelist *pagelist)
144 {
145         osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
146         osd_data->pagelist = pagelist;
147 }
148
149 #ifdef CONFIG_BLOCK
150 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
151                         struct bio *bio, size_t bio_length)
152 {
153         osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
154         osd_data->bio = bio;
155         osd_data->bio_length = bio_length;
156 }
157 #endif /* CONFIG_BLOCK */
158
159 #define osd_req_op_data(oreq, whch, typ, fld)                           \
160 ({                                                                      \
161         struct ceph_osd_request *__oreq = (oreq);                       \
162         unsigned int __whch = (whch);                                   \
163         BUG_ON(__whch >= __oreq->r_num_ops);                            \
164         &__oreq->r_ops[__whch].typ.fld;                                 \
165 })
166
167 static struct ceph_osd_data *
168 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
169 {
170         BUG_ON(which >= osd_req->r_num_ops);
171
172         return &osd_req->r_ops[which].raw_data_in;
173 }
174
175 struct ceph_osd_data *
176 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
177                         unsigned int which)
178 {
179         return osd_req_op_data(osd_req, which, extent, osd_data);
180 }
181 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
182
183 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
184                         unsigned int which, struct page **pages,
185                         u64 length, u32 alignment,
186                         bool pages_from_pool, bool own_pages)
187 {
188         struct ceph_osd_data *osd_data;
189
190         osd_data = osd_req_op_raw_data_in(osd_req, which);
191         ceph_osd_data_pages_init(osd_data, pages, length, alignment,
192                                 pages_from_pool, own_pages);
193 }
194 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
195
196 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
197                         unsigned int which, struct page **pages,
198                         u64 length, u32 alignment,
199                         bool pages_from_pool, bool own_pages)
200 {
201         struct ceph_osd_data *osd_data;
202
203         osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
204         ceph_osd_data_pages_init(osd_data, pages, length, alignment,
205                                 pages_from_pool, own_pages);
206 }
207 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
208
209 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
210                         unsigned int which, struct ceph_pagelist *pagelist)
211 {
212         struct ceph_osd_data *osd_data;
213
214         osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
215         ceph_osd_data_pagelist_init(osd_data, pagelist);
216 }
217 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
218
219 #ifdef CONFIG_BLOCK
220 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
221                         unsigned int which, struct bio *bio, size_t bio_length)
222 {
223         struct ceph_osd_data *osd_data;
224
225         osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
226         ceph_osd_data_bio_init(osd_data, bio, bio_length);
227 }
228 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
229 #endif /* CONFIG_BLOCK */
230
231 static void osd_req_op_cls_request_info_pagelist(
232                         struct ceph_osd_request *osd_req,
233                         unsigned int which, struct ceph_pagelist *pagelist)
234 {
235         struct ceph_osd_data *osd_data;
236
237         osd_data = osd_req_op_data(osd_req, which, cls, request_info);
238         ceph_osd_data_pagelist_init(osd_data, pagelist);
239 }
240
241 void osd_req_op_cls_request_data_pagelist(
242                         struct ceph_osd_request *osd_req,
243                         unsigned int which, struct ceph_pagelist *pagelist)
244 {
245         struct ceph_osd_data *osd_data;
246
247         osd_data = osd_req_op_data(osd_req, which, cls, request_data);
248         ceph_osd_data_pagelist_init(osd_data, pagelist);
249         osd_req->r_ops[which].cls.indata_len += pagelist->length;
250         osd_req->r_ops[which].indata_len += pagelist->length;
251 }
252 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
253
254 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
255                         unsigned int which, struct page **pages, u64 length,
256                         u32 alignment, bool pages_from_pool, bool own_pages)
257 {
258         struct ceph_osd_data *osd_data;
259
260         osd_data = osd_req_op_data(osd_req, which, cls, request_data);
261         ceph_osd_data_pages_init(osd_data, pages, length, alignment,
262                                 pages_from_pool, own_pages);
263         osd_req->r_ops[which].cls.indata_len += length;
264         osd_req->r_ops[which].indata_len += length;
265 }
266 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
267
268 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
269                         unsigned int which, struct page **pages, u64 length,
270                         u32 alignment, bool pages_from_pool, bool own_pages)
271 {
272         struct ceph_osd_data *osd_data;
273
274         osd_data = osd_req_op_data(osd_req, which, cls, response_data);
275         ceph_osd_data_pages_init(osd_data, pages, length, alignment,
276                                 pages_from_pool, own_pages);
277 }
278 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
279
280 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
281 {
282         switch (osd_data->type) {
283         case CEPH_OSD_DATA_TYPE_NONE:
284                 return 0;
285         case CEPH_OSD_DATA_TYPE_PAGES:
286                 return osd_data->length;
287         case CEPH_OSD_DATA_TYPE_PAGELIST:
288                 return (u64)osd_data->pagelist->length;
289 #ifdef CONFIG_BLOCK
290         case CEPH_OSD_DATA_TYPE_BIO:
291                 return (u64)osd_data->bio_length;
292 #endif /* CONFIG_BLOCK */
293         default:
294                 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
295                 return 0;
296         }
297 }
298
299 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
300 {
301         if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
302                 int num_pages;
303
304                 num_pages = calc_pages_for((u64)osd_data->alignment,
305                                                 (u64)osd_data->length);
306                 ceph_release_page_vector(osd_data->pages, num_pages);
307         }
308         ceph_osd_data_init(osd_data);
309 }
310
311 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
312                         unsigned int which)
313 {
314         struct ceph_osd_req_op *op;
315
316         BUG_ON(which >= osd_req->r_num_ops);
317         op = &osd_req->r_ops[which];
318
319         switch (op->op) {
320         case CEPH_OSD_OP_READ:
321         case CEPH_OSD_OP_WRITE:
322         case CEPH_OSD_OP_WRITEFULL:
323                 ceph_osd_data_release(&op->extent.osd_data);
324                 break;
325         case CEPH_OSD_OP_CALL:
326                 ceph_osd_data_release(&op->cls.request_info);
327                 ceph_osd_data_release(&op->cls.request_data);
328                 ceph_osd_data_release(&op->cls.response_data);
329                 break;
330         case CEPH_OSD_OP_SETXATTR:
331         case CEPH_OSD_OP_CMPXATTR:
332                 ceph_osd_data_release(&op->xattr.osd_data);
333                 break;
334         case CEPH_OSD_OP_STAT:
335                 ceph_osd_data_release(&op->raw_data_in);
336                 break;
337         case CEPH_OSD_OP_NOTIFY_ACK:
338                 ceph_osd_data_release(&op->notify_ack.request_data);
339                 break;
340         case CEPH_OSD_OP_NOTIFY:
341                 ceph_osd_data_release(&op->notify.request_data);
342                 ceph_osd_data_release(&op->notify.response_data);
343                 break;
344         case CEPH_OSD_OP_LIST_WATCHERS:
345                 ceph_osd_data_release(&op->list_watchers.response_data);
346                 break;
347         default:
348                 break;
349         }
350 }
351
352 /*
353  * Assumes @t is zero-initialized.
354  */
355 static void target_init(struct ceph_osd_request_target *t)
356 {
357         ceph_oid_init(&t->base_oid);
358         ceph_oloc_init(&t->base_oloc);
359         ceph_oid_init(&t->target_oid);
360         ceph_oloc_init(&t->target_oloc);
361
362         ceph_osds_init(&t->acting);
363         ceph_osds_init(&t->up);
364         t->size = -1;
365         t->min_size = -1;
366
367         t->osd = CEPH_HOMELESS_OSD;
368 }
369
370 static void target_copy(struct ceph_osd_request_target *dest,
371                         const struct ceph_osd_request_target *src)
372 {
373         ceph_oid_copy(&dest->base_oid, &src->base_oid);
374         ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
375         ceph_oid_copy(&dest->target_oid, &src->target_oid);
376         ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
377
378         dest->pgid = src->pgid; /* struct */
379         dest->spgid = src->spgid; /* struct */
380         dest->pg_num = src->pg_num;
381         dest->pg_num_mask = src->pg_num_mask;
382         ceph_osds_copy(&dest->acting, &src->acting);
383         ceph_osds_copy(&dest->up, &src->up);
384         dest->size = src->size;
385         dest->min_size = src->min_size;
386         dest->sort_bitwise = src->sort_bitwise;
387
388         dest->flags = src->flags;
389         dest->paused = src->paused;
390
391         dest->epoch = src->epoch;
392         dest->last_force_resend = src->last_force_resend;
393
394         dest->osd = src->osd;
395 }
396
397 static void target_destroy(struct ceph_osd_request_target *t)
398 {
399         ceph_oid_destroy(&t->base_oid);
400         ceph_oloc_destroy(&t->base_oloc);
401         ceph_oid_destroy(&t->target_oid);
402         ceph_oloc_destroy(&t->target_oloc);
403 }
404
405 /*
406  * requests
407  */
408 static void request_release_checks(struct ceph_osd_request *req)
409 {
410         WARN_ON(!RB_EMPTY_NODE(&req->r_node));
411         WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
412         WARN_ON(!list_empty(&req->r_unsafe_item));
413         WARN_ON(req->r_osd);
414 }
415
416 static void ceph_osdc_release_request(struct kref *kref)
417 {
418         struct ceph_osd_request *req = container_of(kref,
419                                             struct ceph_osd_request, r_kref);
420         unsigned int which;
421
422         dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
423              req->r_request, req->r_reply);
424         request_release_checks(req);
425
426         if (req->r_request)
427                 ceph_msg_put(req->r_request);
428         if (req->r_reply)
429                 ceph_msg_put(req->r_reply);
430
431         for (which = 0; which < req->r_num_ops; which++)
432                 osd_req_op_data_release(req, which);
433
434         target_destroy(&req->r_t);
435         ceph_put_snap_context(req->r_snapc);
436
437         if (req->r_mempool)
438                 mempool_free(req, req->r_osdc->req_mempool);
439         else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
440                 kmem_cache_free(ceph_osd_request_cache, req);
441         else
442                 kfree(req);
443 }
444
445 void ceph_osdc_get_request(struct ceph_osd_request *req)
446 {
447         dout("%s %p (was %d)\n", __func__, req,
448              kref_read(&req->r_kref));
449         kref_get(&req->r_kref);
450 }
451 EXPORT_SYMBOL(ceph_osdc_get_request);
452
453 void ceph_osdc_put_request(struct ceph_osd_request *req)
454 {
455         if (req) {
456                 dout("%s %p (was %d)\n", __func__, req,
457                      kref_read(&req->r_kref));
458                 kref_put(&req->r_kref, ceph_osdc_release_request);
459         }
460 }
461 EXPORT_SYMBOL(ceph_osdc_put_request);
462
463 static void request_init(struct ceph_osd_request *req)
464 {
465         /* req only, each op is zeroed in _osd_req_op_init() */
466         memset(req, 0, sizeof(*req));
467
468         kref_init(&req->r_kref);
469         init_completion(&req->r_completion);
470         RB_CLEAR_NODE(&req->r_node);
471         RB_CLEAR_NODE(&req->r_mc_node);
472         INIT_LIST_HEAD(&req->r_unsafe_item);
473
474         target_init(&req->r_t);
475 }
476
477 /*
478  * This is ugly, but it allows us to reuse linger registration and ping
479  * requests, keeping the structure of the code around send_linger{_ping}()
480  * reasonable.  Setting up a min_nr=2 mempool for each linger request
481  * and dealing with copying ops (this blasts req only, watch op remains
482  * intact) isn't any better.
483  */
484 static void request_reinit(struct ceph_osd_request *req)
485 {
486         struct ceph_osd_client *osdc = req->r_osdc;
487         bool mempool = req->r_mempool;
488         unsigned int num_ops = req->r_num_ops;
489         u64 snapid = req->r_snapid;
490         struct ceph_snap_context *snapc = req->r_snapc;
491         bool linger = req->r_linger;
492         struct ceph_msg *request_msg = req->r_request;
493         struct ceph_msg *reply_msg = req->r_reply;
494
495         dout("%s req %p\n", __func__, req);
496         WARN_ON(kref_read(&req->r_kref) != 1);
497         request_release_checks(req);
498
499         WARN_ON(kref_read(&request_msg->kref) != 1);
500         WARN_ON(kref_read(&reply_msg->kref) != 1);
501         target_destroy(&req->r_t);
502
503         request_init(req);
504         req->r_osdc = osdc;
505         req->r_mempool = mempool;
506         req->r_num_ops = num_ops;
507         req->r_snapid = snapid;
508         req->r_snapc = snapc;
509         req->r_linger = linger;
510         req->r_request = request_msg;
511         req->r_reply = reply_msg;
512 }
513
514 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
515                                                struct ceph_snap_context *snapc,
516                                                unsigned int num_ops,
517                                                bool use_mempool,
518                                                gfp_t gfp_flags)
519 {
520         struct ceph_osd_request *req;
521
522         if (use_mempool) {
523                 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
524                 req = mempool_alloc(osdc->req_mempool, gfp_flags);
525         } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
526                 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
527         } else {
528                 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
529                 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
530                               gfp_flags);
531         }
532         if (unlikely(!req))
533                 return NULL;
534
535         request_init(req);
536         req->r_osdc = osdc;
537         req->r_mempool = use_mempool;
538         req->r_num_ops = num_ops;
539         req->r_snapid = CEPH_NOSNAP;
540         req->r_snapc = ceph_get_snap_context(snapc);
541
542         dout("%s req %p\n", __func__, req);
543         return req;
544 }
545 EXPORT_SYMBOL(ceph_osdc_alloc_request);
546
547 static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
548 {
549         return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
550 }
551
552 int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
553 {
554         struct ceph_osd_client *osdc = req->r_osdc;
555         struct ceph_msg *msg;
556         int msg_size;
557
558         WARN_ON(ceph_oid_empty(&req->r_base_oid));
559         WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
560
561         /* create request message */
562         msg_size = CEPH_ENCODING_START_BLK_LEN +
563                         CEPH_PGID_ENCODING_LEN + 1; /* spgid */
564         msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
565         msg_size += CEPH_ENCODING_START_BLK_LEN +
566                         sizeof(struct ceph_osd_reqid); /* reqid */
567         msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
568         msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
569         msg_size += CEPH_ENCODING_START_BLK_LEN +
570                         ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
571         msg_size += 4 + req->r_base_oid.name_len; /* oid */
572         msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
573         msg_size += 8; /* snapid */
574         msg_size += 8; /* snap_seq */
575         msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
576         msg_size += 4 + 8; /* retry_attempt, features */
577
578         if (req->r_mempool)
579                 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
580         else
581                 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
582         if (!msg)
583                 return -ENOMEM;
584
585         memset(msg->front.iov_base, 0, msg->front.iov_len);
586         req->r_request = msg;
587
588         /* create reply message */
589         msg_size = OSD_OPREPLY_FRONT_LEN;
590         msg_size += req->r_base_oid.name_len;
591         msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
592
593         if (req->r_mempool)
594                 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
595         else
596                 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
597         if (!msg)
598                 return -ENOMEM;
599
600         req->r_reply = msg;
601
602         return 0;
603 }
604 EXPORT_SYMBOL(ceph_osdc_alloc_messages);
605
606 static bool osd_req_opcode_valid(u16 opcode)
607 {
608         switch (opcode) {
609 #define GENERATE_CASE(op, opcode, str)  case CEPH_OSD_OP_##op: return true;
610 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
611 #undef GENERATE_CASE
612         default:
613                 return false;
614         }
615 }
616
617 /*
618  * This is an osd op init function for opcodes that have no data or
619  * other information associated with them.  It also serves as a
620  * common init routine for all the other init functions, below.
621  */
622 static struct ceph_osd_req_op *
623 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
624                  u16 opcode, u32 flags)
625 {
626         struct ceph_osd_req_op *op;
627
628         BUG_ON(which >= osd_req->r_num_ops);
629         BUG_ON(!osd_req_opcode_valid(opcode));
630
631         op = &osd_req->r_ops[which];
632         memset(op, 0, sizeof (*op));
633         op->op = opcode;
634         op->flags = flags;
635
636         return op;
637 }
638
639 void osd_req_op_init(struct ceph_osd_request *osd_req,
640                      unsigned int which, u16 opcode, u32 flags)
641 {
642         (void)_osd_req_op_init(osd_req, which, opcode, flags);
643 }
644 EXPORT_SYMBOL(osd_req_op_init);
645
646 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
647                                 unsigned int which, u16 opcode,
648                                 u64 offset, u64 length,
649                                 u64 truncate_size, u32 truncate_seq)
650 {
651         struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
652                                                       opcode, 0);
653         size_t payload_len = 0;
654
655         BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
656                opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
657                opcode != CEPH_OSD_OP_TRUNCATE);
658
659         op->extent.offset = offset;
660         op->extent.length = length;
661         op->extent.truncate_size = truncate_size;
662         op->extent.truncate_seq = truncate_seq;
663         if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
664                 payload_len += length;
665
666         op->indata_len = payload_len;
667 }
668 EXPORT_SYMBOL(osd_req_op_extent_init);
669
670 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
671                                 unsigned int which, u64 length)
672 {
673         struct ceph_osd_req_op *op;
674         u64 previous;
675
676         BUG_ON(which >= osd_req->r_num_ops);
677         op = &osd_req->r_ops[which];
678         previous = op->extent.length;
679
680         if (length == previous)
681                 return;         /* Nothing to do */
682         BUG_ON(length > previous);
683
684         op->extent.length = length;
685         if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
686                 op->indata_len -= previous - length;
687 }
688 EXPORT_SYMBOL(osd_req_op_extent_update);
689
690 void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
691                                 unsigned int which, u64 offset_inc)
692 {
693         struct ceph_osd_req_op *op, *prev_op;
694
695         BUG_ON(which + 1 >= osd_req->r_num_ops);
696
697         prev_op = &osd_req->r_ops[which];
698         op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
699         /* dup previous one */
700         op->indata_len = prev_op->indata_len;
701         op->outdata_len = prev_op->outdata_len;
702         op->extent = prev_op->extent;
703         /* adjust offset */
704         op->extent.offset += offset_inc;
705         op->extent.length -= offset_inc;
706
707         if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
708                 op->indata_len -= offset_inc;
709 }
710 EXPORT_SYMBOL(osd_req_op_extent_dup_last);
711
712 void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
713                         u16 opcode, const char *class, const char *method)
714 {
715         struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
716                                                       opcode, 0);
717         struct ceph_pagelist *pagelist;
718         size_t payload_len = 0;
719         size_t size;
720
721         BUG_ON(opcode != CEPH_OSD_OP_CALL);
722
723         pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
724         BUG_ON(!pagelist);
725         ceph_pagelist_init(pagelist);
726
727         op->cls.class_name = class;
728         size = strlen(class);
729         BUG_ON(size > (size_t) U8_MAX);
730         op->cls.class_len = size;
731         ceph_pagelist_append(pagelist, class, size);
732         payload_len += size;
733
734         op->cls.method_name = method;
735         size = strlen(method);
736         BUG_ON(size > (size_t) U8_MAX);
737         op->cls.method_len = size;
738         ceph_pagelist_append(pagelist, method, size);
739         payload_len += size;
740
741         osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
742
743         op->indata_len = payload_len;
744 }
745 EXPORT_SYMBOL(osd_req_op_cls_init);
746
747 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
748                           u16 opcode, const char *name, const void *value,
749                           size_t size, u8 cmp_op, u8 cmp_mode)
750 {
751         struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
752                                                       opcode, 0);
753         struct ceph_pagelist *pagelist;
754         size_t payload_len;
755
756         BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
757
758         pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
759         if (!pagelist)
760                 return -ENOMEM;
761
762         ceph_pagelist_init(pagelist);
763
764         payload_len = strlen(name);
765         op->xattr.name_len = payload_len;
766         ceph_pagelist_append(pagelist, name, payload_len);
767
768         op->xattr.value_len = size;
769         ceph_pagelist_append(pagelist, value, size);
770         payload_len += size;
771
772         op->xattr.cmp_op = cmp_op;
773         op->xattr.cmp_mode = cmp_mode;
774
775         ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
776         op->indata_len = payload_len;
777         return 0;
778 }
779 EXPORT_SYMBOL(osd_req_op_xattr_init);
780
781 /*
782  * @watch_opcode: CEPH_OSD_WATCH_OP_*
783  */
784 static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
785                                   u64 cookie, u8 watch_opcode)
786 {
787         struct ceph_osd_req_op *op;
788
789         op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
790         op->watch.cookie = cookie;
791         op->watch.op = watch_opcode;
792         op->watch.gen = 0;
793 }
794
795 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
796                                 unsigned int which,
797                                 u64 expected_object_size,
798                                 u64 expected_write_size)
799 {
800         struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
801                                                       CEPH_OSD_OP_SETALLOCHINT,
802                                                       0);
803
804         op->alloc_hint.expected_object_size = expected_object_size;
805         op->alloc_hint.expected_write_size = expected_write_size;
806
807         /*
808          * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
809          * not worth a feature bit.  Set FAILOK per-op flag to make
810          * sure older osds don't trip over an unsupported opcode.
811          */
812         op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
813 }
814 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
815
816 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
817                                 struct ceph_osd_data *osd_data)
818 {
819         u64 length = ceph_osd_data_length(osd_data);
820
821         if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
822                 BUG_ON(length > (u64) SIZE_MAX);
823                 if (length)
824                         ceph_msg_data_add_pages(msg, osd_data->pages,
825                                         length, osd_data->alignment);
826         } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
827                 BUG_ON(!length);
828                 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
829 #ifdef CONFIG_BLOCK
830         } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
831                 ceph_msg_data_add_bio(msg, osd_data->bio, length);
832 #endif
833         } else {
834                 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
835         }
836 }
837
838 static u32 osd_req_encode_op(struct ceph_osd_op *dst,
839                              const struct ceph_osd_req_op *src)
840 {
841         if (WARN_ON(!osd_req_opcode_valid(src->op))) {
842                 pr_err("unrecognized osd opcode %d\n", src->op);
843
844                 return 0;
845         }
846
847         switch (src->op) {
848         case CEPH_OSD_OP_STAT:
849                 break;
850         case CEPH_OSD_OP_READ:
851         case CEPH_OSD_OP_WRITE:
852         case CEPH_OSD_OP_WRITEFULL:
853         case CEPH_OSD_OP_ZERO:
854         case CEPH_OSD_OP_TRUNCATE:
855                 dst->extent.offset = cpu_to_le64(src->extent.offset);
856                 dst->extent.length = cpu_to_le64(src->extent.length);
857                 dst->extent.truncate_size =
858                         cpu_to_le64(src->extent.truncate_size);
859                 dst->extent.truncate_seq =
860                         cpu_to_le32(src->extent.truncate_seq);
861                 break;
862         case CEPH_OSD_OP_CALL:
863                 dst->cls.class_len = src->cls.class_len;
864                 dst->cls.method_len = src->cls.method_len;
865                 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
866                 break;
867         case CEPH_OSD_OP_WATCH:
868                 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
869                 dst->watch.ver = cpu_to_le64(0);
870                 dst->watch.op = src->watch.op;
871                 dst->watch.gen = cpu_to_le32(src->watch.gen);
872                 break;
873         case CEPH_OSD_OP_NOTIFY_ACK:
874                 break;
875         case CEPH_OSD_OP_NOTIFY:
876                 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
877                 break;
878         case CEPH_OSD_OP_LIST_WATCHERS:
879                 break;
880         case CEPH_OSD_OP_SETALLOCHINT:
881                 dst->alloc_hint.expected_object_size =
882                     cpu_to_le64(src->alloc_hint.expected_object_size);
883                 dst->alloc_hint.expected_write_size =
884                     cpu_to_le64(src->alloc_hint.expected_write_size);
885                 break;
886         case CEPH_OSD_OP_SETXATTR:
887         case CEPH_OSD_OP_CMPXATTR:
888                 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
889                 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
890                 dst->xattr.cmp_op = src->xattr.cmp_op;
891                 dst->xattr.cmp_mode = src->xattr.cmp_mode;
892                 break;
893         case CEPH_OSD_OP_CREATE:
894         case CEPH_OSD_OP_DELETE:
895                 break;
896         default:
897                 pr_err("unsupported osd opcode %s\n",
898                         ceph_osd_op_name(src->op));
899                 WARN_ON(1);
900
901                 return 0;
902         }
903
904         dst->op = cpu_to_le16(src->op);
905         dst->flags = cpu_to_le32(src->flags);
906         dst->payload_len = cpu_to_le32(src->indata_len);
907
908         return src->indata_len;
909 }
910
911 /*
912  * build new request AND message, calculate layout, and adjust file
913  * extent as needed.
914  *
915  * if the file was recently truncated, we include information about its
916  * old and new size so that the object can be updated appropriately.  (we
917  * avoid synchronously deleting truncated objects because it's slow.)
918  */
919 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
920                                                struct ceph_file_layout *layout,
921                                                struct ceph_vino vino,
922                                                u64 off, u64 *plen,
923                                                unsigned int which, int num_ops,
924                                                int opcode, int flags,
925                                                struct ceph_snap_context *snapc,
926                                                u32 truncate_seq,
927                                                u64 truncate_size,
928                                                bool use_mempool)
929 {
930         struct ceph_osd_request *req;
931         u64 objnum = 0;
932         u64 objoff = 0;
933         u64 objlen = 0;
934         int r;
935
936         BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
937                opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
938                opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
939
940         req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
941                                         GFP_NOFS);
942         if (!req) {
943                 r = -ENOMEM;
944                 goto fail;
945         }
946
947         /* calculate max write size */
948         r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
949         if (r)
950                 goto fail;
951
952         if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
953                 osd_req_op_init(req, which, opcode, 0);
954         } else {
955                 u32 object_size = layout->object_size;
956                 u32 object_base = off - objoff;
957                 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
958                         if (truncate_size <= object_base) {
959                                 truncate_size = 0;
960                         } else {
961                                 truncate_size -= object_base;
962                                 if (truncate_size > object_size)
963                                         truncate_size = object_size;
964                         }
965                 }
966                 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
967                                        truncate_size, truncate_seq);
968         }
969
970         req->r_abort_on_full = true;
971         req->r_flags = flags;
972         req->r_base_oloc.pool = layout->pool_id;
973         req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
974         ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
975
976         req->r_snapid = vino.snap;
977         if (flags & CEPH_OSD_FLAG_WRITE)
978                 req->r_data_offset = off;
979
980         r = ceph_osdc_alloc_messages(req, GFP_NOFS);
981         if (r)
982                 goto fail;
983
984         return req;
985
986 fail:
987         ceph_osdc_put_request(req);
988         return ERR_PTR(r);
989 }
990 EXPORT_SYMBOL(ceph_osdc_new_request);
991
992 /*
993  * We keep osd requests in an rbtree, sorted by ->r_tid.
994  */
995 DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
996 DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
997
998 static bool osd_homeless(struct ceph_osd *osd)
999 {
1000         return osd->o_osd == CEPH_HOMELESS_OSD;
1001 }
1002
1003 static bool osd_registered(struct ceph_osd *osd)
1004 {
1005         verify_osdc_locked(osd->o_osdc);
1006
1007         return !RB_EMPTY_NODE(&osd->o_node);
1008 }
1009
1010 /*
1011  * Assumes @osd is zero-initialized.
1012  */
1013 static void osd_init(struct ceph_osd *osd)
1014 {
1015         refcount_set(&osd->o_ref, 1);
1016         RB_CLEAR_NODE(&osd->o_node);
1017         osd->o_requests = RB_ROOT;
1018         osd->o_linger_requests = RB_ROOT;
1019         osd->o_backoff_mappings = RB_ROOT;
1020         osd->o_backoffs_by_id = RB_ROOT;
1021         INIT_LIST_HEAD(&osd->o_osd_lru);
1022         INIT_LIST_HEAD(&osd->o_keepalive_item);
1023         osd->o_incarnation = 1;
1024         mutex_init(&osd->lock);
1025 }
1026
1027 static void osd_cleanup(struct ceph_osd *osd)
1028 {
1029         WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
1030         WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1031         WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1032         WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1033         WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
1034         WARN_ON(!list_empty(&osd->o_osd_lru));
1035         WARN_ON(!list_empty(&osd->o_keepalive_item));
1036
1037         if (osd->o_auth.authorizer) {
1038                 WARN_ON(osd_homeless(osd));
1039                 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1040         }
1041 }
1042
1043 /*
1044  * Track open sessions with osds.
1045  */
1046 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1047 {
1048         struct ceph_osd *osd;
1049
1050         WARN_ON(onum == CEPH_HOMELESS_OSD);
1051
1052         osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
1053         osd_init(osd);
1054         osd->o_osdc = osdc;
1055         osd->o_osd = onum;
1056
1057         ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1058
1059         return osd;
1060 }
1061
1062 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1063 {
1064         if (refcount_inc_not_zero(&osd->o_ref)) {
1065                 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1066                      refcount_read(&osd->o_ref));
1067                 return osd;
1068         } else {
1069                 dout("get_osd %p FAIL\n", osd);
1070                 return NULL;
1071         }
1072 }
1073
1074 static void put_osd(struct ceph_osd *osd)
1075 {
1076         dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1077              refcount_read(&osd->o_ref) - 1);
1078         if (refcount_dec_and_test(&osd->o_ref)) {
1079                 osd_cleanup(osd);
1080                 kfree(osd);
1081         }
1082 }
1083
1084 DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1085
1086 static void __move_osd_to_lru(struct ceph_osd *osd)
1087 {
1088         struct ceph_osd_client *osdc = osd->o_osdc;
1089
1090         dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1091         BUG_ON(!list_empty(&osd->o_osd_lru));
1092
1093         spin_lock(&osdc->osd_lru_lock);
1094         list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1095         spin_unlock(&osdc->osd_lru_lock);
1096
1097         osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1098 }
1099
1100 static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1101 {
1102         if (RB_EMPTY_ROOT(&osd->o_requests) &&
1103             RB_EMPTY_ROOT(&osd->o_linger_requests))
1104                 __move_osd_to_lru(osd);
1105 }
1106
1107 static void __remove_osd_from_lru(struct ceph_osd *osd)
1108 {
1109         struct ceph_osd_client *osdc = osd->o_osdc;
1110
1111         dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1112
1113         spin_lock(&osdc->osd_lru_lock);
1114         if (!list_empty(&osd->o_osd_lru))
1115                 list_del_init(&osd->o_osd_lru);
1116         spin_unlock(&osdc->osd_lru_lock);
1117 }
1118
1119 /*
1120  * Close the connection and assign any leftover requests to the
1121  * homeless session.
1122  */
1123 static void close_osd(struct ceph_osd *osd)
1124 {
1125         struct ceph_osd_client *osdc = osd->o_osdc;
1126         struct rb_node *n;
1127
1128         verify_osdc_wrlocked(osdc);
1129         dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1130
1131         ceph_con_close(&osd->o_con);
1132
1133         for (n = rb_first(&osd->o_requests); n; ) {
1134                 struct ceph_osd_request *req =
1135                     rb_entry(n, struct ceph_osd_request, r_node);
1136
1137                 n = rb_next(n); /* unlink_request() */
1138
1139                 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1140                 unlink_request(osd, req);
1141                 link_request(&osdc->homeless_osd, req);
1142         }
1143         for (n = rb_first(&osd->o_linger_requests); n; ) {
1144                 struct ceph_osd_linger_request *lreq =
1145                     rb_entry(n, struct ceph_osd_linger_request, node);
1146
1147                 n = rb_next(n); /* unlink_linger() */
1148
1149                 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1150                      lreq->linger_id);
1151                 unlink_linger(osd, lreq);
1152                 link_linger(&osdc->homeless_osd, lreq);
1153         }
1154         clear_backoffs(osd);
1155
1156         __remove_osd_from_lru(osd);
1157         erase_osd(&osdc->osds, osd);
1158         put_osd(osd);
1159 }
1160
1161 /*
1162  * reset osd connect
1163  */
1164 static int reopen_osd(struct ceph_osd *osd)
1165 {
1166         struct ceph_entity_addr *peer_addr;
1167
1168         dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1169
1170         if (RB_EMPTY_ROOT(&osd->o_requests) &&
1171             RB_EMPTY_ROOT(&osd->o_linger_requests)) {
1172                 close_osd(osd);
1173                 return -ENODEV;
1174         }
1175
1176         peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1177         if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1178                         !ceph_con_opened(&osd->o_con)) {
1179                 struct rb_node *n;
1180
1181                 dout("osd addr hasn't changed and connection never opened, "
1182                      "letting msgr retry\n");
1183                 /* touch each r_stamp for handle_timeout()'s benfit */
1184                 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1185                         struct ceph_osd_request *req =
1186                             rb_entry(n, struct ceph_osd_request, r_node);
1187                         req->r_stamp = jiffies;
1188                 }
1189
1190                 return -EAGAIN;
1191         }
1192
1193         ceph_con_close(&osd->o_con);
1194         ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1195         osd->o_incarnation++;
1196
1197         return 0;
1198 }
1199
1200 static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1201                                           bool wrlocked)
1202 {
1203         struct ceph_osd *osd;
1204
1205         if (wrlocked)
1206                 verify_osdc_wrlocked(osdc);
1207         else
1208                 verify_osdc_locked(osdc);
1209
1210         if (o != CEPH_HOMELESS_OSD)
1211                 osd = lookup_osd(&osdc->osds, o);
1212         else
1213                 osd = &osdc->homeless_osd;
1214         if (!osd) {
1215                 if (!wrlocked)
1216                         return ERR_PTR(-EAGAIN);
1217
1218                 osd = create_osd(osdc, o);
1219                 insert_osd(&osdc->osds, osd);
1220                 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1221                               &osdc->osdmap->osd_addr[osd->o_osd]);
1222         }
1223
1224         dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1225         return osd;
1226 }
1227
1228 /*
1229  * Create request <-> OSD session relation.
1230  *
1231  * @req has to be assigned a tid, @osd may be homeless.
1232  */
1233 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1234 {
1235         verify_osd_locked(osd);
1236         WARN_ON(!req->r_tid || req->r_osd);
1237         dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1238              req, req->r_tid);
1239
1240         if (!osd_homeless(osd))
1241                 __remove_osd_from_lru(osd);
1242         else
1243                 atomic_inc(&osd->o_osdc->num_homeless);
1244
1245         get_osd(osd);
1246         insert_request(&osd->o_requests, req);
1247         req->r_osd = osd;
1248 }
1249
1250 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1251 {
1252         verify_osd_locked(osd);
1253         WARN_ON(req->r_osd != osd);
1254         dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1255              req, req->r_tid);
1256
1257         req->r_osd = NULL;
1258         erase_request(&osd->o_requests, req);
1259         put_osd(osd);
1260
1261         if (!osd_homeless(osd))
1262                 maybe_move_osd_to_lru(osd);
1263         else
1264                 atomic_dec(&osd->o_osdc->num_homeless);
1265 }
1266
1267 static bool __pool_full(struct ceph_pg_pool_info *pi)
1268 {
1269         return pi->flags & CEPH_POOL_FLAG_FULL;
1270 }
1271
1272 static bool have_pool_full(struct ceph_osd_client *osdc)
1273 {
1274         struct rb_node *n;
1275
1276         for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1277                 struct ceph_pg_pool_info *pi =
1278                     rb_entry(n, struct ceph_pg_pool_info, node);
1279
1280                 if (__pool_full(pi))
1281                         return true;
1282         }
1283
1284         return false;
1285 }
1286
1287 static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1288 {
1289         struct ceph_pg_pool_info *pi;
1290
1291         pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1292         if (!pi)
1293                 return false;
1294
1295         return __pool_full(pi);
1296 }
1297
1298 /*
1299  * Returns whether a request should be blocked from being sent
1300  * based on the current osdmap and osd_client settings.
1301  */
1302 static bool target_should_be_paused(struct ceph_osd_client *osdc,
1303                                     const struct ceph_osd_request_target *t,
1304                                     struct ceph_pg_pool_info *pi)
1305 {
1306         bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1307         bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1308                        ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1309                        __pool_full(pi);
1310
1311         WARN_ON(pi->id != t->target_oloc.pool);
1312         return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1313                ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1314                (osdc->osdmap->epoch < osdc->epoch_barrier);
1315 }
1316
1317 enum calc_target_result {
1318         CALC_TARGET_NO_ACTION = 0,
1319         CALC_TARGET_NEED_RESEND,
1320         CALC_TARGET_POOL_DNE,
1321 };
1322
1323 static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1324                                            struct ceph_osd_request_target *t,
1325                                            struct ceph_connection *con,
1326                                            bool any_change)
1327 {
1328         struct ceph_pg_pool_info *pi;
1329         struct ceph_pg pgid, last_pgid;
1330         struct ceph_osds up, acting;
1331         bool force_resend = false;
1332         bool unpaused = false;
1333         bool legacy_change;
1334         bool split = false;
1335         bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1336         bool recovery_deletes = ceph_osdmap_flag(osdc,
1337                                                  CEPH_OSDMAP_RECOVERY_DELETES);
1338         enum calc_target_result ct_res;
1339         int ret;
1340
1341         t->epoch = osdc->osdmap->epoch;
1342         pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1343         if (!pi) {
1344                 t->osd = CEPH_HOMELESS_OSD;
1345                 ct_res = CALC_TARGET_POOL_DNE;
1346                 goto out;
1347         }
1348
1349         if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1350                 if (t->last_force_resend < pi->last_force_request_resend) {
1351                         t->last_force_resend = pi->last_force_request_resend;
1352                         force_resend = true;
1353                 } else if (t->last_force_resend == 0) {
1354                         force_resend = true;
1355                 }
1356         }
1357
1358         /* apply tiering */
1359         ceph_oid_copy(&t->target_oid, &t->base_oid);
1360         ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1361         if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1362                 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1363                         t->target_oloc.pool = pi->read_tier;
1364                 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1365                         t->target_oloc.pool = pi->write_tier;
1366
1367                 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1368                 if (!pi) {
1369                         t->osd = CEPH_HOMELESS_OSD;
1370                         ct_res = CALC_TARGET_POOL_DNE;
1371                         goto out;
1372                 }
1373         }
1374
1375         ret = __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc,
1376                                           &pgid);
1377         if (ret) {
1378                 WARN_ON(ret != -ENOENT);
1379                 t->osd = CEPH_HOMELESS_OSD;
1380                 ct_res = CALC_TARGET_POOL_DNE;
1381                 goto out;
1382         }
1383         last_pgid.pool = pgid.pool;
1384         last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1385
1386         ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
1387         if (any_change &&
1388             ceph_is_new_interval(&t->acting,
1389                                  &acting,
1390                                  &t->up,
1391                                  &up,
1392                                  t->size,
1393                                  pi->size,
1394                                  t->min_size,
1395                                  pi->min_size,
1396                                  t->pg_num,
1397                                  pi->pg_num,
1398                                  t->sort_bitwise,
1399                                  sort_bitwise,
1400                                  t->recovery_deletes,
1401                                  recovery_deletes,
1402                                  &last_pgid))
1403                 force_resend = true;
1404
1405         if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1406                 t->paused = false;
1407                 unpaused = true;
1408         }
1409         legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1410                         ceph_osds_changed(&t->acting, &acting, any_change);
1411         if (t->pg_num)
1412                 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
1413
1414         if (legacy_change || force_resend || split) {
1415                 t->pgid = pgid; /* struct */
1416                 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
1417                 ceph_osds_copy(&t->acting, &acting);
1418                 ceph_osds_copy(&t->up, &up);
1419                 t->size = pi->size;
1420                 t->min_size = pi->min_size;
1421                 t->pg_num = pi->pg_num;
1422                 t->pg_num_mask = pi->pg_num_mask;
1423                 t->sort_bitwise = sort_bitwise;
1424                 t->recovery_deletes = recovery_deletes;
1425
1426                 t->osd = acting.primary;
1427         }
1428
1429         if (unpaused || legacy_change || force_resend ||
1430             (split && con && CEPH_HAVE_FEATURE(con->peer_features,
1431                                                RESEND_ON_SPLIT)))
1432                 ct_res = CALC_TARGET_NEED_RESEND;
1433         else
1434                 ct_res = CALC_TARGET_NO_ACTION;
1435
1436 out:
1437         dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1438         return ct_res;
1439 }
1440
1441 static struct ceph_spg_mapping *alloc_spg_mapping(void)
1442 {
1443         struct ceph_spg_mapping *spg;
1444
1445         spg = kmalloc(sizeof(*spg), GFP_NOIO);
1446         if (!spg)
1447                 return NULL;
1448
1449         RB_CLEAR_NODE(&spg->node);
1450         spg->backoffs = RB_ROOT;
1451         return spg;
1452 }
1453
1454 static void free_spg_mapping(struct ceph_spg_mapping *spg)
1455 {
1456         WARN_ON(!RB_EMPTY_NODE(&spg->node));
1457         WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1458
1459         kfree(spg);
1460 }
1461
1462 /*
1463  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1464  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1465  * defined only within a specific spgid; it does not pass anything to
1466  * children on split, or to another primary.
1467  */
1468 DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1469                  RB_BYPTR, const struct ceph_spg *, node)
1470
1471 static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1472 {
1473         return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1474 }
1475
1476 static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1477                                    void **pkey, size_t *pkey_len)
1478 {
1479         if (hoid->key_len) {
1480                 *pkey = hoid->key;
1481                 *pkey_len = hoid->key_len;
1482         } else {
1483                 *pkey = hoid->oid;
1484                 *pkey_len = hoid->oid_len;
1485         }
1486 }
1487
1488 static int compare_names(const void *name1, size_t name1_len,
1489                          const void *name2, size_t name2_len)
1490 {
1491         int ret;
1492
1493         ret = memcmp(name1, name2, min(name1_len, name2_len));
1494         if (!ret) {
1495                 if (name1_len < name2_len)
1496                         ret = -1;
1497                 else if (name1_len > name2_len)
1498                         ret = 1;
1499         }
1500         return ret;
1501 }
1502
1503 static int hoid_compare(const struct ceph_hobject_id *lhs,
1504                         const struct ceph_hobject_id *rhs)
1505 {
1506         void *effective_key1, *effective_key2;
1507         size_t effective_key1_len, effective_key2_len;
1508         int ret;
1509
1510         if (lhs->is_max < rhs->is_max)
1511                 return -1;
1512         if (lhs->is_max > rhs->is_max)
1513                 return 1;
1514
1515         if (lhs->pool < rhs->pool)
1516                 return -1;
1517         if (lhs->pool > rhs->pool)
1518                 return 1;
1519
1520         if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1521                 return -1;
1522         if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1523                 return 1;
1524
1525         ret = compare_names(lhs->nspace, lhs->nspace_len,
1526                             rhs->nspace, rhs->nspace_len);
1527         if (ret)
1528                 return ret;
1529
1530         hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1531         hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1532         ret = compare_names(effective_key1, effective_key1_len,
1533                             effective_key2, effective_key2_len);
1534         if (ret)
1535                 return ret;
1536
1537         ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1538         if (ret)
1539                 return ret;
1540
1541         if (lhs->snapid < rhs->snapid)
1542                 return -1;
1543         if (lhs->snapid > rhs->snapid)
1544                 return 1;
1545
1546         return 0;
1547 }
1548
1549 /*
1550  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1551  * compat stuff here.
1552  *
1553  * Assumes @hoid is zero-initialized.
1554  */
1555 static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1556 {
1557         u8 struct_v;
1558         u32 struct_len;
1559         int ret;
1560
1561         ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1562                                   &struct_len);
1563         if (ret)
1564                 return ret;
1565
1566         if (struct_v < 4) {
1567                 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1568                 goto e_inval;
1569         }
1570
1571         hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1572                                                 GFP_NOIO);
1573         if (IS_ERR(hoid->key)) {
1574                 ret = PTR_ERR(hoid->key);
1575                 hoid->key = NULL;
1576                 return ret;
1577         }
1578
1579         hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1580                                                 GFP_NOIO);
1581         if (IS_ERR(hoid->oid)) {
1582                 ret = PTR_ERR(hoid->oid);
1583                 hoid->oid = NULL;
1584                 return ret;
1585         }
1586
1587         ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1588         ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1589         ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1590
1591         hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1592                                                    GFP_NOIO);
1593         if (IS_ERR(hoid->nspace)) {
1594                 ret = PTR_ERR(hoid->nspace);
1595                 hoid->nspace = NULL;
1596                 return ret;
1597         }
1598
1599         ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1600
1601         ceph_hoid_build_hash_cache(hoid);
1602         return 0;
1603
1604 e_inval:
1605         return -EINVAL;
1606 }
1607
1608 static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1609 {
1610         return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1611                4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1612 }
1613
1614 static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1615 {
1616         ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1617         ceph_encode_string(p, end, hoid->key, hoid->key_len);
1618         ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1619         ceph_encode_64(p, hoid->snapid);
1620         ceph_encode_32(p, hoid->hash);
1621         ceph_encode_8(p, hoid->is_max);
1622         ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1623         ceph_encode_64(p, hoid->pool);
1624 }
1625
1626 static void free_hoid(struct ceph_hobject_id *hoid)
1627 {
1628         if (hoid) {
1629                 kfree(hoid->key);
1630                 kfree(hoid->oid);
1631                 kfree(hoid->nspace);
1632                 kfree(hoid);
1633         }
1634 }
1635
1636 static struct ceph_osd_backoff *alloc_backoff(void)
1637 {
1638         struct ceph_osd_backoff *backoff;
1639
1640         backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1641         if (!backoff)
1642                 return NULL;
1643
1644         RB_CLEAR_NODE(&backoff->spg_node);
1645         RB_CLEAR_NODE(&backoff->id_node);
1646         return backoff;
1647 }
1648
1649 static void free_backoff(struct ceph_osd_backoff *backoff)
1650 {
1651         WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1652         WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1653
1654         free_hoid(backoff->begin);
1655         free_hoid(backoff->end);
1656         kfree(backoff);
1657 }
1658
1659 /*
1660  * Within a specific spgid, backoffs are managed by ->begin hoid.
1661  */
1662 DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1663                         RB_BYVAL, spg_node);
1664
1665 static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1666                                             const struct ceph_hobject_id *hoid)
1667 {
1668         struct rb_node *n = root->rb_node;
1669
1670         while (n) {
1671                 struct ceph_osd_backoff *cur =
1672                     rb_entry(n, struct ceph_osd_backoff, spg_node);
1673                 int cmp;
1674
1675                 cmp = hoid_compare(hoid, cur->begin);
1676                 if (cmp < 0) {
1677                         n = n->rb_left;
1678                 } else if (cmp > 0) {
1679                         if (hoid_compare(hoid, cur->end) < 0)
1680                                 return cur;
1681
1682                         n = n->rb_right;
1683                 } else {
1684                         return cur;
1685                 }
1686         }
1687
1688         return NULL;
1689 }
1690
1691 /*
1692  * Each backoff has a unique id within its OSD session.
1693  */
1694 DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1695
1696 static void clear_backoffs(struct ceph_osd *osd)
1697 {
1698         while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1699                 struct ceph_spg_mapping *spg =
1700                     rb_entry(rb_first(&osd->o_backoff_mappings),
1701                              struct ceph_spg_mapping, node);
1702
1703                 while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1704                         struct ceph_osd_backoff *backoff =
1705                             rb_entry(rb_first(&spg->backoffs),
1706                                      struct ceph_osd_backoff, spg_node);
1707
1708                         erase_backoff(&spg->backoffs, backoff);
1709                         erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1710                         free_backoff(backoff);
1711                 }
1712                 erase_spg_mapping(&osd->o_backoff_mappings, spg);
1713                 free_spg_mapping(spg);
1714         }
1715 }
1716
1717 /*
1718  * Set up a temporary, non-owning view into @t.
1719  */
1720 static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1721                                   const struct ceph_osd_request_target *t)
1722 {
1723         hoid->key = NULL;
1724         hoid->key_len = 0;
1725         hoid->oid = t->target_oid.name;
1726         hoid->oid_len = t->target_oid.name_len;
1727         hoid->snapid = CEPH_NOSNAP;
1728         hoid->hash = t->pgid.seed;
1729         hoid->is_max = false;
1730         if (t->target_oloc.pool_ns) {
1731                 hoid->nspace = t->target_oloc.pool_ns->str;
1732                 hoid->nspace_len = t->target_oloc.pool_ns->len;
1733         } else {
1734                 hoid->nspace = NULL;
1735                 hoid->nspace_len = 0;
1736         }
1737         hoid->pool = t->target_oloc.pool;
1738         ceph_hoid_build_hash_cache(hoid);
1739 }
1740
1741 static bool should_plug_request(struct ceph_osd_request *req)
1742 {
1743         struct ceph_osd *osd = req->r_osd;
1744         struct ceph_spg_mapping *spg;
1745         struct ceph_osd_backoff *backoff;
1746         struct ceph_hobject_id hoid;
1747
1748         spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1749         if (!spg)
1750                 return false;
1751
1752         hoid_fill_from_target(&hoid, &req->r_t);
1753         backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1754         if (!backoff)
1755                 return false;
1756
1757         dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1758              __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1759              backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1760         return true;
1761 }
1762
1763 static void setup_request_data(struct ceph_osd_request *req,
1764                                struct ceph_msg *msg)
1765 {
1766         u32 data_len = 0;
1767         int i;
1768
1769         if (!list_empty(&msg->data))
1770                 return;
1771
1772         WARN_ON(msg->data_length);
1773         for (i = 0; i < req->r_num_ops; i++) {
1774                 struct ceph_osd_req_op *op = &req->r_ops[i];
1775
1776                 switch (op->op) {
1777                 /* request */
1778                 case CEPH_OSD_OP_WRITE:
1779                 case CEPH_OSD_OP_WRITEFULL:
1780                         WARN_ON(op->indata_len != op->extent.length);
1781                         ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1782                         break;
1783                 case CEPH_OSD_OP_SETXATTR:
1784                 case CEPH_OSD_OP_CMPXATTR:
1785                         WARN_ON(op->indata_len != op->xattr.name_len +
1786                                                   op->xattr.value_len);
1787                         ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1788                         break;
1789                 case CEPH_OSD_OP_NOTIFY_ACK:
1790                         ceph_osdc_msg_data_add(msg,
1791                                                &op->notify_ack.request_data);
1792                         break;
1793
1794                 /* reply */
1795                 case CEPH_OSD_OP_STAT:
1796                         ceph_osdc_msg_data_add(req->r_reply,
1797                                                &op->raw_data_in);
1798                         break;
1799                 case CEPH_OSD_OP_READ:
1800                         ceph_osdc_msg_data_add(req->r_reply,
1801                                                &op->extent.osd_data);
1802                         break;
1803                 case CEPH_OSD_OP_LIST_WATCHERS:
1804                         ceph_osdc_msg_data_add(req->r_reply,
1805                                                &op->list_watchers.response_data);
1806                         break;
1807
1808                 /* both */
1809                 case CEPH_OSD_OP_CALL:
1810                         WARN_ON(op->indata_len != op->cls.class_len +
1811                                                   op->cls.method_len +
1812                                                   op->cls.indata_len);
1813                         ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1814                         /* optional, can be NONE */
1815                         ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1816                         /* optional, can be NONE */
1817                         ceph_osdc_msg_data_add(req->r_reply,
1818                                                &op->cls.response_data);
1819                         break;
1820                 case CEPH_OSD_OP_NOTIFY:
1821                         ceph_osdc_msg_data_add(msg,
1822                                                &op->notify.request_data);
1823                         ceph_osdc_msg_data_add(req->r_reply,
1824                                                &op->notify.response_data);
1825                         break;
1826                 }
1827
1828                 data_len += op->indata_len;
1829         }
1830
1831         WARN_ON(data_len != msg->data_length);
1832 }
1833
1834 static void encode_pgid(void **p, const struct ceph_pg *pgid)
1835 {
1836         ceph_encode_8(p, 1);
1837         ceph_encode_64(p, pgid->pool);
1838         ceph_encode_32(p, pgid->seed);
1839         ceph_encode_32(p, -1); /* preferred */
1840 }
1841
1842 static void encode_spgid(void **p, const struct ceph_spg *spgid)
1843 {
1844         ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
1845         encode_pgid(p, &spgid->pgid);
1846         ceph_encode_8(p, spgid->shard);
1847 }
1848
1849 static void encode_oloc(void **p, void *end,
1850                         const struct ceph_object_locator *oloc)
1851 {
1852         ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
1853         ceph_encode_64(p, oloc->pool);
1854         ceph_encode_32(p, -1); /* preferred */
1855         ceph_encode_32(p, 0);  /* key len */
1856         if (oloc->pool_ns)
1857                 ceph_encode_string(p, end, oloc->pool_ns->str,
1858                                    oloc->pool_ns->len);
1859         else
1860                 ceph_encode_32(p, 0);
1861 }
1862
1863 static void encode_request_partial(struct ceph_osd_request *req,
1864                                    struct ceph_msg *msg)
1865 {
1866         void *p = msg->front.iov_base;
1867         void *const end = p + msg->front_alloc_len;
1868         u32 data_len = 0;
1869         int i;
1870
1871         if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1872                 /* snapshots aren't writeable */
1873                 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1874         } else {
1875                 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1876                         req->r_data_offset || req->r_snapc);
1877         }
1878
1879         setup_request_data(req, msg);
1880
1881         encode_spgid(&p, &req->r_t.spgid); /* actual spg */
1882         ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
1883         ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1884         ceph_encode_32(&p, req->r_flags);
1885
1886         /* reqid */
1887         ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
1888         memset(p, 0, sizeof(struct ceph_osd_reqid));
1889         p += sizeof(struct ceph_osd_reqid);
1890
1891         /* trace */
1892         memset(p, 0, sizeof(struct ceph_blkin_trace_info));
1893         p += sizeof(struct ceph_blkin_trace_info);
1894
1895         ceph_encode_32(&p, 0); /* client_inc, always 0 */
1896         ceph_encode_timespec(p, &req->r_mtime);
1897         p += sizeof(struct ceph_timespec);
1898
1899         encode_oloc(&p, end, &req->r_t.target_oloc);
1900         ceph_encode_string(&p, end, req->r_t.target_oid.name,
1901                            req->r_t.target_oid.name_len);
1902
1903         /* ops, can imply data */
1904         ceph_encode_16(&p, req->r_num_ops);
1905         for (i = 0; i < req->r_num_ops; i++) {
1906                 data_len += osd_req_encode_op(p, &req->r_ops[i]);
1907                 p += sizeof(struct ceph_osd_op);
1908         }
1909
1910         ceph_encode_64(&p, req->r_snapid); /* snapid */
1911         if (req->r_snapc) {
1912                 ceph_encode_64(&p, req->r_snapc->seq);
1913                 ceph_encode_32(&p, req->r_snapc->num_snaps);
1914                 for (i = 0; i < req->r_snapc->num_snaps; i++)
1915                         ceph_encode_64(&p, req->r_snapc->snaps[i]);
1916         } else {
1917                 ceph_encode_64(&p, 0); /* snap_seq */
1918                 ceph_encode_32(&p, 0); /* snaps len */
1919         }
1920
1921         ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1922         BUG_ON(p > end - 8); /* space for features */
1923
1924         msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
1925         /* front_len is finalized in encode_request_finish() */
1926         msg->front.iov_len = p - msg->front.iov_base;
1927         msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1928         msg->hdr.data_len = cpu_to_le32(data_len);
1929         /*
1930          * The header "data_off" is a hint to the receiver allowing it
1931          * to align received data into its buffers such that there's no
1932          * need to re-copy it before writing it to disk (direct I/O).
1933          */
1934         msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1935
1936         dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
1937              req->r_t.target_oid.name, req->r_t.target_oid.name_len);
1938 }
1939
1940 static void encode_request_finish(struct ceph_msg *msg)
1941 {
1942         void *p = msg->front.iov_base;
1943         void *const partial_end = p + msg->front.iov_len;
1944         void *const end = p + msg->front_alloc_len;
1945
1946         if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
1947                 /* luminous OSD -- encode features and be done */
1948                 p = partial_end;
1949                 ceph_encode_64(&p, msg->con->peer_features);
1950         } else {
1951                 struct {
1952                         char spgid[CEPH_ENCODING_START_BLK_LEN +
1953                                    CEPH_PGID_ENCODING_LEN + 1];
1954                         __le32 hash;
1955                         __le32 epoch;
1956                         __le32 flags;
1957                         char reqid[CEPH_ENCODING_START_BLK_LEN +
1958                                    sizeof(struct ceph_osd_reqid)];
1959                         char trace[sizeof(struct ceph_blkin_trace_info)];
1960                         __le32 client_inc;
1961                         struct ceph_timespec mtime;
1962                 } __packed head;
1963                 struct ceph_pg pgid;
1964                 void *oloc, *oid, *tail;
1965                 int oloc_len, oid_len, tail_len;
1966                 int len;
1967
1968                 /*
1969                  * Pre-luminous OSD -- reencode v8 into v4 using @head
1970                  * as a temporary buffer.  Encode the raw PG; the rest
1971                  * is just a matter of moving oloc, oid and tail blobs
1972                  * around.
1973                  */
1974                 memcpy(&head, p, sizeof(head));
1975                 p += sizeof(head);
1976
1977                 oloc = p;
1978                 p += CEPH_ENCODING_START_BLK_LEN;
1979                 pgid.pool = ceph_decode_64(&p);
1980                 p += 4 + 4; /* preferred, key len */
1981                 len = ceph_decode_32(&p);
1982                 p += len;   /* nspace */
1983                 oloc_len = p - oloc;
1984
1985                 oid = p;
1986                 len = ceph_decode_32(&p);
1987                 p += len;
1988                 oid_len = p - oid;
1989
1990                 tail = p;
1991                 tail_len = partial_end - p;
1992
1993                 p = msg->front.iov_base;
1994                 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
1995                 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
1996                 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
1997                 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
1998
1999                 /* reassert_version */
2000                 memset(p, 0, sizeof(struct ceph_eversion));
2001                 p += sizeof(struct ceph_eversion);
2002
2003                 BUG_ON(p >= oloc);
2004                 memmove(p, oloc, oloc_len);
2005                 p += oloc_len;
2006
2007                 pgid.seed = le32_to_cpu(head.hash);
2008                 encode_pgid(&p, &pgid); /* raw pg */
2009
2010                 BUG_ON(p >= oid);
2011                 memmove(p, oid, oid_len);
2012                 p += oid_len;
2013
2014                 /* tail -- ops, snapid, snapc, retry_attempt */
2015                 BUG_ON(p >= tail);
2016                 memmove(p, tail, tail_len);
2017                 p += tail_len;
2018
2019                 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
2020         }
2021
2022         BUG_ON(p > end);
2023         msg->front.iov_len = p - msg->front.iov_base;
2024         msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2025
2026         dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
2027              le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
2028              le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
2029              le16_to_cpu(msg->hdr.version));
2030 }
2031
2032 /*
2033  * @req has to be assigned a tid and registered.
2034  */
2035 static void send_request(struct ceph_osd_request *req)
2036 {
2037         struct ceph_osd *osd = req->r_osd;
2038
2039         verify_osd_locked(osd);
2040         WARN_ON(osd->o_osd != req->r_t.osd);
2041
2042         /* backoff? */
2043         if (should_plug_request(req))
2044                 return;
2045
2046         /*
2047          * We may have a previously queued request message hanging
2048          * around.  Cancel it to avoid corrupting the msgr.
2049          */
2050         if (req->r_sent)
2051                 ceph_msg_revoke(req->r_request);
2052
2053         req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2054         if (req->r_attempts)
2055                 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2056         else
2057                 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2058
2059         encode_request_partial(req, req->r_request);
2060
2061         dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
2062              __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2063              req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
2064              req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
2065              req->r_attempts);
2066
2067         req->r_t.paused = false;
2068         req->r_stamp = jiffies;
2069         req->r_attempts++;
2070
2071         req->r_sent = osd->o_incarnation;
2072         req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2073         ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
2074 }
2075
2076 static void maybe_request_map(struct ceph_osd_client *osdc)
2077 {
2078         bool continuous = false;
2079
2080         verify_osdc_locked(osdc);
2081         WARN_ON(!osdc->osdmap->epoch);
2082
2083         if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2084             ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2085             ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2086                 dout("%s osdc %p continuous\n", __func__, osdc);
2087                 continuous = true;
2088         } else {
2089                 dout("%s osdc %p onetime\n", __func__, osdc);
2090         }
2091
2092         if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2093                                osdc->osdmap->epoch + 1, continuous))
2094                 ceph_monc_renew_subs(&osdc->client->monc);
2095 }
2096
2097 static void complete_request(struct ceph_osd_request *req, int err);
2098 static void send_map_check(struct ceph_osd_request *req);
2099
2100 static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
2101 {
2102         struct ceph_osd_client *osdc = req->r_osdc;
2103         struct ceph_osd *osd;
2104         enum calc_target_result ct_res;
2105         bool need_send = false;
2106         bool promoted = false;
2107         bool need_abort = false;
2108
2109         WARN_ON(req->r_tid);
2110         dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
2111
2112 again:
2113         ct_res = calc_target(osdc, &req->r_t, NULL, false);
2114         if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
2115                 goto promote;
2116
2117         osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
2118         if (IS_ERR(osd)) {
2119                 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
2120                 goto promote;
2121         }
2122
2123         if (osdc->osdmap->epoch < osdc->epoch_barrier) {
2124                 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
2125                      osdc->epoch_barrier);
2126                 req->r_t.paused = true;
2127                 maybe_request_map(osdc);
2128         } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2129                    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2130                 dout("req %p pausewr\n", req);
2131                 req->r_t.paused = true;
2132                 maybe_request_map(osdc);
2133         } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2134                    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2135                 dout("req %p pauserd\n", req);
2136                 req->r_t.paused = true;
2137                 maybe_request_map(osdc);
2138         } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2139                    !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
2140                                      CEPH_OSD_FLAG_FULL_FORCE)) &&
2141                    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2142                     pool_full(osdc, req->r_t.base_oloc.pool))) {
2143                 dout("req %p full/pool_full\n", req);
2144                 pr_warn_ratelimited("FULL or reached pool quota\n");
2145                 req->r_t.paused = true;
2146                 maybe_request_map(osdc);
2147                 if (req->r_abort_on_full)
2148                         need_abort = true;
2149         } else if (!osd_homeless(osd)) {
2150                 need_send = true;
2151         } else {
2152                 maybe_request_map(osdc);
2153         }
2154
2155         mutex_lock(&osd->lock);
2156         /*
2157          * Assign the tid atomically with send_request() to protect
2158          * multiple writes to the same object from racing with each
2159          * other, resulting in out of order ops on the OSDs.
2160          */
2161         req->r_tid = atomic64_inc_return(&osdc->last_tid);
2162         link_request(osd, req);
2163         if (need_send)
2164                 send_request(req);
2165         else if (need_abort)
2166                 complete_request(req, -ENOSPC);
2167         mutex_unlock(&osd->lock);
2168
2169         if (ct_res == CALC_TARGET_POOL_DNE)
2170                 send_map_check(req);
2171
2172         if (promoted)
2173                 downgrade_write(&osdc->lock);
2174         return;
2175
2176 promote:
2177         up_read(&osdc->lock);
2178         down_write(&osdc->lock);
2179         wrlocked = true;
2180         promoted = true;
2181         goto again;
2182 }
2183
2184 static void account_request(struct ceph_osd_request *req)
2185 {
2186         WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2187         WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
2188
2189         req->r_flags |= CEPH_OSD_FLAG_ONDISK;
2190         atomic_inc(&req->r_osdc->num_requests);
2191
2192         req->r_start_stamp = jiffies;
2193 }
2194
2195 static void submit_request(struct ceph_osd_request *req, bool wrlocked)
2196 {
2197         ceph_osdc_get_request(req);
2198         account_request(req);
2199         __submit_request(req, wrlocked);
2200 }
2201
2202 static void finish_request(struct ceph_osd_request *req)
2203 {
2204         struct ceph_osd_client *osdc = req->r_osdc;
2205
2206         WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
2207         dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2208
2209         if (req->r_osd)
2210                 unlink_request(req->r_osd, req);
2211         atomic_dec(&osdc->num_requests);
2212
2213         /*
2214          * If an OSD has failed or returned and a request has been sent
2215          * twice, it's possible to get a reply and end up here while the
2216          * request message is queued for delivery.  We will ignore the
2217          * reply, so not a big deal, but better to try and catch it.
2218          */
2219         ceph_msg_revoke(req->r_request);
2220         ceph_msg_revoke_incoming(req->r_reply);
2221 }
2222
2223 static void __complete_request(struct ceph_osd_request *req)
2224 {
2225         if (req->r_callback) {
2226                 dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2227                      req->r_tid, req->r_callback, req->r_result);
2228                 req->r_callback(req);
2229         }
2230 }
2231
2232 /*
2233  * This is open-coded in handle_reply().
2234  */
2235 static void complete_request(struct ceph_osd_request *req, int err)
2236 {
2237         dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2238
2239         req->r_result = err;
2240         finish_request(req);
2241         __complete_request(req);
2242         complete_all(&req->r_completion);
2243         ceph_osdc_put_request(req);
2244 }
2245
2246 static void cancel_map_check(struct ceph_osd_request *req)
2247 {
2248         struct ceph_osd_client *osdc = req->r_osdc;
2249         struct ceph_osd_request *lookup_req;
2250
2251         verify_osdc_wrlocked(osdc);
2252
2253         lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2254         if (!lookup_req)
2255                 return;
2256
2257         WARN_ON(lookup_req != req);
2258         erase_request_mc(&osdc->map_checks, req);
2259         ceph_osdc_put_request(req);
2260 }
2261
2262 static void cancel_request(struct ceph_osd_request *req)
2263 {
2264         dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2265
2266         cancel_map_check(req);
2267         finish_request(req);
2268         complete_all(&req->r_completion);
2269         ceph_osdc_put_request(req);
2270 }
2271
2272 static void abort_request(struct ceph_osd_request *req, int err)
2273 {
2274         dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2275
2276         cancel_map_check(req);
2277         complete_request(req, err);
2278 }
2279
2280 static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2281 {
2282         if (likely(eb > osdc->epoch_barrier)) {
2283                 dout("updating epoch_barrier from %u to %u\n",
2284                                 osdc->epoch_barrier, eb);
2285                 osdc->epoch_barrier = eb;
2286                 /* Request map if we're not to the barrier yet */
2287                 if (eb > osdc->osdmap->epoch)
2288                         maybe_request_map(osdc);
2289         }
2290 }
2291
2292 void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2293 {
2294         down_read(&osdc->lock);
2295         if (unlikely(eb > osdc->epoch_barrier)) {
2296                 up_read(&osdc->lock);
2297                 down_write(&osdc->lock);
2298                 update_epoch_barrier(osdc, eb);
2299                 up_write(&osdc->lock);
2300         } else {
2301                 up_read(&osdc->lock);
2302         }
2303 }
2304 EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
2305
2306 /*
2307  * Drop all pending requests that are stalled waiting on a full condition to
2308  * clear, and complete them with ENOSPC as the return code. Set the
2309  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2310  * cancelled.
2311  */
2312 static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2313 {
2314         struct rb_node *n;
2315         bool victims = false;
2316
2317         dout("enter abort_on_full\n");
2318
2319         if (!ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) && !have_pool_full(osdc))
2320                 goto out;
2321
2322         /* Scan list and see if there is anything to abort */
2323         for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2324                 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2325                 struct rb_node *m;
2326
2327                 m = rb_first(&osd->o_requests);
2328                 while (m) {
2329                         struct ceph_osd_request *req = rb_entry(m,
2330                                         struct ceph_osd_request, r_node);
2331                         m = rb_next(m);
2332
2333                         if (req->r_abort_on_full) {
2334                                 victims = true;
2335                                 break;
2336                         }
2337                 }
2338                 if (victims)
2339                         break;
2340         }
2341
2342         if (!victims)
2343                 goto out;
2344
2345         /*
2346          * Update the barrier to current epoch if it's behind that point,
2347          * since we know we have some calls to be aborted in the tree.
2348          */
2349         update_epoch_barrier(osdc, osdc->osdmap->epoch);
2350
2351         for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2352                 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2353                 struct rb_node *m;
2354
2355                 m = rb_first(&osd->o_requests);
2356                 while (m) {
2357                         struct ceph_osd_request *req = rb_entry(m,
2358                                         struct ceph_osd_request, r_node);
2359                         m = rb_next(m);
2360
2361                         if (req->r_abort_on_full &&
2362                             (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2363                              pool_full(osdc, req->r_t.target_oloc.pool)))
2364                                 abort_request(req, -ENOSPC);
2365                 }
2366         }
2367 out:
2368         dout("return abort_on_full barrier=%u\n", osdc->epoch_barrier);
2369 }
2370
2371 static void check_pool_dne(struct ceph_osd_request *req)
2372 {
2373         struct ceph_osd_client *osdc = req->r_osdc;
2374         struct ceph_osdmap *map = osdc->osdmap;
2375
2376         verify_osdc_wrlocked(osdc);
2377         WARN_ON(!map->epoch);
2378
2379         if (req->r_attempts) {
2380                 /*
2381                  * We sent a request earlier, which means that
2382                  * previously the pool existed, and now it does not
2383                  * (i.e., it was deleted).
2384                  */
2385                 req->r_map_dne_bound = map->epoch;
2386                 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2387                      req->r_tid);
2388         } else {
2389                 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2390                      req, req->r_tid, req->r_map_dne_bound, map->epoch);
2391         }
2392
2393         if (req->r_map_dne_bound) {
2394                 if (map->epoch >= req->r_map_dne_bound) {
2395                         /* we had a new enough map */
2396                         pr_info_ratelimited("tid %llu pool does not exist\n",
2397                                             req->r_tid);
2398                         complete_request(req, -ENOENT);
2399                 }
2400         } else {
2401                 send_map_check(req);
2402         }
2403 }
2404
2405 static void map_check_cb(struct ceph_mon_generic_request *greq)
2406 {
2407         struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2408         struct ceph_osd_request *req;
2409         u64 tid = greq->private_data;
2410
2411         WARN_ON(greq->result || !greq->u.newest);
2412
2413         down_write(&osdc->lock);
2414         req = lookup_request_mc(&osdc->map_checks, tid);
2415         if (!req) {
2416                 dout("%s tid %llu dne\n", __func__, tid);
2417                 goto out_unlock;
2418         }
2419
2420         dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2421              req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2422         if (!req->r_map_dne_bound)
2423                 req->r_map_dne_bound = greq->u.newest;
2424         erase_request_mc(&osdc->map_checks, req);
2425         check_pool_dne(req);
2426
2427         ceph_osdc_put_request(req);
2428 out_unlock:
2429         up_write(&osdc->lock);
2430 }
2431
2432 static void send_map_check(struct ceph_osd_request *req)
2433 {
2434         struct ceph_osd_client *osdc = req->r_osdc;
2435         struct ceph_osd_request *lookup_req;
2436         int ret;
2437
2438         verify_osdc_wrlocked(osdc);
2439
2440         lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2441         if (lookup_req) {
2442                 WARN_ON(lookup_req != req);
2443                 return;
2444         }
2445
2446         ceph_osdc_get_request(req);
2447         insert_request_mc(&osdc->map_checks, req);
2448         ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2449                                           map_check_cb, req->r_tid);
2450         WARN_ON(ret);
2451 }
2452
2453 /*
2454  * lingering requests, watch/notify v2 infrastructure
2455  */
2456 static void linger_release(struct kref *kref)
2457 {
2458         struct ceph_osd_linger_request *lreq =
2459             container_of(kref, struct ceph_osd_linger_request, kref);
2460
2461         dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2462              lreq->reg_req, lreq->ping_req);
2463         WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2464         WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
2465         WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2466         WARN_ON(!list_empty(&lreq->scan_item));
2467         WARN_ON(!list_empty(&lreq->pending_lworks));
2468         WARN_ON(lreq->osd);
2469
2470         if (lreq->reg_req)
2471                 ceph_osdc_put_request(lreq->reg_req);
2472         if (lreq->ping_req)
2473                 ceph_osdc_put_request(lreq->ping_req);
2474         target_destroy(&lreq->t);
2475         kfree(lreq);
2476 }
2477
2478 static void linger_put(struct ceph_osd_linger_request *lreq)
2479 {
2480         if (lreq)
2481                 kref_put(&lreq->kref, linger_release);
2482 }
2483
2484 static struct ceph_osd_linger_request *
2485 linger_get(struct ceph_osd_linger_request *lreq)
2486 {
2487         kref_get(&lreq->kref);
2488         return lreq;
2489 }
2490
2491 static struct ceph_osd_linger_request *
2492 linger_alloc(struct ceph_osd_client *osdc)
2493 {
2494         struct ceph_osd_linger_request *lreq;
2495
2496         lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2497         if (!lreq)
2498                 return NULL;
2499
2500         kref_init(&lreq->kref);
2501         mutex_init(&lreq->lock);
2502         RB_CLEAR_NODE(&lreq->node);
2503         RB_CLEAR_NODE(&lreq->osdc_node);
2504         RB_CLEAR_NODE(&lreq->mc_node);
2505         INIT_LIST_HEAD(&lreq->scan_item);
2506         INIT_LIST_HEAD(&lreq->pending_lworks);
2507         init_completion(&lreq->reg_commit_wait);
2508         init_completion(&lreq->notify_finish_wait);
2509
2510         lreq->osdc = osdc;
2511         target_init(&lreq->t);
2512
2513         dout("%s lreq %p\n", __func__, lreq);
2514         return lreq;
2515 }
2516
2517 DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2518 DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
2519 DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2520
2521 /*
2522  * Create linger request <-> OSD session relation.
2523  *
2524  * @lreq has to be registered, @osd may be homeless.
2525  */
2526 static void link_linger(struct ceph_osd *osd,
2527                         struct ceph_osd_linger_request *lreq)
2528 {
2529         verify_osd_locked(osd);
2530         WARN_ON(!lreq->linger_id || lreq->osd);
2531         dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2532              osd->o_osd, lreq, lreq->linger_id);
2533
2534         if (!osd_homeless(osd))
2535                 __remove_osd_from_lru(osd);
2536         else
2537                 atomic_inc(&osd->o_osdc->num_homeless);
2538
2539         get_osd(osd);
2540         insert_linger(&osd->o_linger_requests, lreq);
2541         lreq->osd = osd;
2542 }
2543
2544 static void unlink_linger(struct ceph_osd *osd,
2545                           struct ceph_osd_linger_request *lreq)
2546 {
2547         verify_osd_locked(osd);
2548         WARN_ON(lreq->osd != osd);
2549         dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2550              osd->o_osd, lreq, lreq->linger_id);
2551
2552         lreq->osd = NULL;
2553         erase_linger(&osd->o_linger_requests, lreq);
2554         put_osd(osd);
2555
2556         if (!osd_homeless(osd))
2557                 maybe_move_osd_to_lru(osd);
2558         else
2559                 atomic_dec(&osd->o_osdc->num_homeless);
2560 }
2561
2562 static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2563 {
2564         verify_osdc_locked(lreq->osdc);
2565
2566         return !RB_EMPTY_NODE(&lreq->osdc_node);
2567 }
2568
2569 static bool linger_registered(struct ceph_osd_linger_request *lreq)
2570 {
2571         struct ceph_osd_client *osdc = lreq->osdc;
2572         bool registered;
2573
2574         down_read(&osdc->lock);
2575         registered = __linger_registered(lreq);
2576         up_read(&osdc->lock);
2577
2578         return registered;
2579 }
2580
2581 static void linger_register(struct ceph_osd_linger_request *lreq)
2582 {
2583         struct ceph_osd_client *osdc = lreq->osdc;
2584
2585         verify_osdc_wrlocked(osdc);
2586         WARN_ON(lreq->linger_id);
2587
2588         linger_get(lreq);
2589         lreq->linger_id = ++osdc->last_linger_id;
2590         insert_linger_osdc(&osdc->linger_requests, lreq);
2591 }
2592
2593 static void linger_unregister(struct ceph_osd_linger_request *lreq)
2594 {
2595         struct ceph_osd_client *osdc = lreq->osdc;
2596
2597         verify_osdc_wrlocked(osdc);
2598
2599         erase_linger_osdc(&osdc->linger_requests, lreq);
2600         linger_put(lreq);
2601 }
2602
2603 static void cancel_linger_request(struct ceph_osd_request *req)
2604 {
2605         struct ceph_osd_linger_request *lreq = req->r_priv;
2606
2607         WARN_ON(!req->r_linger);
2608         cancel_request(req);
2609         linger_put(lreq);
2610 }
2611
2612 struct linger_work {
2613         struct work_struct work;
2614         struct ceph_osd_linger_request *lreq;
2615         struct list_head pending_item;
2616         unsigned long queued_stamp;
2617
2618         union {
2619                 struct {
2620                         u64 notify_id;
2621                         u64 notifier_id;
2622                         void *payload; /* points into @msg front */
2623                         size_t payload_len;
2624
2625                         struct ceph_msg *msg; /* for ceph_msg_put() */
2626                 } notify;
2627                 struct {
2628                         int err;
2629                 } error;
2630         };
2631 };
2632
2633 static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2634                                        work_func_t workfn)
2635 {
2636         struct linger_work *lwork;
2637
2638         lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2639         if (!lwork)
2640                 return NULL;
2641
2642         INIT_WORK(&lwork->work, workfn);
2643         INIT_LIST_HEAD(&lwork->pending_item);
2644         lwork->lreq = linger_get(lreq);
2645
2646         return lwork;
2647 }
2648
2649 static void lwork_free(struct linger_work *lwork)
2650 {
2651         struct ceph_osd_linger_request *lreq = lwork->lreq;
2652
2653         mutex_lock(&lreq->lock);
2654         list_del(&lwork->pending_item);
2655         mutex_unlock(&lreq->lock);
2656
2657         linger_put(lreq);
2658         kfree(lwork);
2659 }
2660
2661 static void lwork_queue(struct linger_work *lwork)
2662 {
2663         struct ceph_osd_linger_request *lreq = lwork->lreq;
2664         struct ceph_osd_client *osdc = lreq->osdc;
2665
2666         verify_lreq_locked(lreq);
2667         WARN_ON(!list_empty(&lwork->pending_item));
2668
2669         lwork->queued_stamp = jiffies;
2670         list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2671         queue_work(osdc->notify_wq, &lwork->work);
2672 }
2673
2674 static void do_watch_notify(struct work_struct *w)
2675 {
2676         struct linger_work *lwork = container_of(w, struct linger_work, work);
2677         struct ceph_osd_linger_request *lreq = lwork->lreq;
2678
2679         if (!linger_registered(lreq)) {
2680                 dout("%s lreq %p not registered\n", __func__, lreq);
2681                 goto out;
2682         }
2683
2684         WARN_ON(!lreq->is_watch);
2685         dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2686              __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2687              lwork->notify.payload_len);
2688         lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2689                   lwork->notify.notifier_id, lwork->notify.payload,
2690                   lwork->notify.payload_len);
2691
2692 out:
2693         ceph_msg_put(lwork->notify.msg);
2694         lwork_free(lwork);
2695 }
2696
2697 static void do_watch_error(struct work_struct *w)
2698 {
2699         struct linger_work *lwork = container_of(w, struct linger_work, work);
2700         struct ceph_osd_linger_request *lreq = lwork->lreq;
2701
2702         if (!linger_registered(lreq)) {
2703                 dout("%s lreq %p not registered\n", __func__, lreq);
2704                 goto out;
2705         }
2706
2707         dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2708         lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2709
2710 out:
2711         lwork_free(lwork);
2712 }
2713
2714 static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2715 {
2716         struct linger_work *lwork;
2717
2718         lwork = lwork_alloc(lreq, do_watch_error);
2719         if (!lwork) {
2720                 pr_err("failed to allocate error-lwork\n");
2721                 return;
2722         }
2723
2724         lwork->error.err = lreq->last_error;
2725         lwork_queue(lwork);
2726 }
2727
2728 static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2729                                        int result)
2730 {
2731         if (!completion_done(&lreq->reg_commit_wait)) {
2732                 lreq->reg_commit_error = (result <= 0 ? result : 0);
2733                 complete_all(&lreq->reg_commit_wait);
2734         }
2735 }
2736
2737 static void linger_commit_cb(struct ceph_osd_request *req)
2738 {
2739         struct ceph_osd_linger_request *lreq = req->r_priv;
2740
2741         mutex_lock(&lreq->lock);
2742         dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2743              lreq->linger_id, req->r_result);
2744         linger_reg_commit_complete(lreq, req->r_result);
2745         lreq->committed = true;
2746
2747         if (!lreq->is_watch) {
2748                 struct ceph_osd_data *osd_data =
2749                     osd_req_op_data(req, 0, notify, response_data);
2750                 void *p = page_address(osd_data->pages[0]);
2751
2752                 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2753                         osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2754
2755                 /* make note of the notify_id */
2756                 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2757                         lreq->notify_id = ceph_decode_64(&p);
2758                         dout("lreq %p notify_id %llu\n", lreq,
2759                              lreq->notify_id);
2760                 } else {
2761                         dout("lreq %p no notify_id\n", lreq);
2762                 }
2763         }
2764
2765         mutex_unlock(&lreq->lock);
2766         linger_put(lreq);
2767 }
2768
2769 static int normalize_watch_error(int err)
2770 {
2771         /*
2772          * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2773          * notification and a failure to reconnect because we raced with
2774          * the delete appear the same to the user.
2775          */
2776         if (err == -ENOENT)
2777                 err = -ENOTCONN;
2778
2779         return err;
2780 }
2781
2782 static void linger_reconnect_cb(struct ceph_osd_request *req)
2783 {
2784         struct ceph_osd_linger_request *lreq = req->r_priv;
2785
2786         mutex_lock(&lreq->lock);
2787         dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2788              lreq, lreq->linger_id, req->r_result, lreq->last_error);
2789         if (req->r_result < 0) {
2790                 if (!lreq->last_error) {
2791                         lreq->last_error = normalize_watch_error(req->r_result);
2792                         queue_watch_error(lreq);
2793                 }
2794         }
2795
2796         mutex_unlock(&lreq->lock);
2797         linger_put(lreq);
2798 }
2799
2800 static void send_linger(struct ceph_osd_linger_request *lreq)
2801 {
2802         struct ceph_osd_request *req = lreq->reg_req;
2803         struct ceph_osd_req_op *op = &req->r_ops[0];
2804
2805         verify_osdc_wrlocked(req->r_osdc);
2806         dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2807
2808         if (req->r_osd)
2809                 cancel_linger_request(req);
2810
2811         request_reinit(req);
2812         ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2813         ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2814         req->r_flags = lreq->t.flags;
2815         req->r_mtime = lreq->mtime;
2816
2817         mutex_lock(&lreq->lock);
2818         if (lreq->is_watch && lreq->committed) {
2819                 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2820                         op->watch.cookie != lreq->linger_id);
2821                 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2822                 op->watch.gen = ++lreq->register_gen;
2823                 dout("lreq %p reconnect register_gen %u\n", lreq,
2824                      op->watch.gen);
2825                 req->r_callback = linger_reconnect_cb;
2826         } else {
2827                 if (!lreq->is_watch)
2828                         lreq->notify_id = 0;
2829                 else
2830                         WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2831                 dout("lreq %p register\n", lreq);
2832                 req->r_callback = linger_commit_cb;
2833         }
2834         mutex_unlock(&lreq->lock);
2835
2836         req->r_priv = linger_get(lreq);
2837         req->r_linger = true;
2838
2839         submit_request(req, true);
2840 }
2841
2842 static void linger_ping_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 ping_sent %lu last_error %d\n",
2848              __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2849              lreq->last_error);
2850         if (lreq->register_gen == req->r_ops[0].watch.gen) {
2851                 if (!req->r_result) {
2852                         lreq->watch_valid_thru = lreq->ping_sent;
2853                 } else if (!lreq->last_error) {
2854                         lreq->last_error = normalize_watch_error(req->r_result);
2855                         queue_watch_error(lreq);
2856                 }
2857         } else {
2858                 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2859                      lreq->register_gen, req->r_ops[0].watch.gen);
2860         }
2861
2862         mutex_unlock(&lreq->lock);
2863         linger_put(lreq);
2864 }
2865
2866 static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2867 {
2868         struct ceph_osd_client *osdc = lreq->osdc;
2869         struct ceph_osd_request *req = lreq->ping_req;
2870         struct ceph_osd_req_op *op = &req->r_ops[0];
2871
2872         if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2873                 dout("%s PAUSERD\n", __func__);
2874                 return;
2875         }
2876
2877         lreq->ping_sent = jiffies;
2878         dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2879              __func__, lreq, lreq->linger_id, lreq->ping_sent,
2880              lreq->register_gen);
2881
2882         if (req->r_osd)
2883                 cancel_linger_request(req);
2884
2885         request_reinit(req);
2886         target_copy(&req->r_t, &lreq->t);
2887
2888         WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2889                 op->watch.cookie != lreq->linger_id ||
2890                 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2891         op->watch.gen = lreq->register_gen;
2892         req->r_callback = linger_ping_cb;
2893         req->r_priv = linger_get(lreq);
2894         req->r_linger = true;
2895
2896         ceph_osdc_get_request(req);
2897         account_request(req);
2898         req->r_tid = atomic64_inc_return(&osdc->last_tid);
2899         link_request(lreq->osd, req);
2900         send_request(req);
2901 }
2902
2903 static void linger_submit(struct ceph_osd_linger_request *lreq)
2904 {
2905         struct ceph_osd_client *osdc = lreq->osdc;
2906         struct ceph_osd *osd;
2907
2908         calc_target(osdc, &lreq->t, NULL, false);
2909         osd = lookup_create_osd(osdc, lreq->t.osd, true);
2910         link_linger(osd, lreq);
2911
2912         send_linger(lreq);
2913 }
2914
2915 static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
2916 {
2917         struct ceph_osd_client *osdc = lreq->osdc;
2918         struct ceph_osd_linger_request *lookup_lreq;
2919
2920         verify_osdc_wrlocked(osdc);
2921
2922         lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2923                                        lreq->linger_id);
2924         if (!lookup_lreq)
2925                 return;
2926
2927         WARN_ON(lookup_lreq != lreq);
2928         erase_linger_mc(&osdc->linger_map_checks, lreq);
2929         linger_put(lreq);
2930 }
2931
2932 /*
2933  * @lreq has to be both registered and linked.
2934  */
2935 static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2936 {
2937         if (lreq->is_watch && lreq->ping_req->r_osd)
2938                 cancel_linger_request(lreq->ping_req);
2939         if (lreq->reg_req->r_osd)
2940                 cancel_linger_request(lreq->reg_req);
2941         cancel_linger_map_check(lreq);
2942         unlink_linger(lreq->osd, lreq);
2943         linger_unregister(lreq);
2944 }
2945
2946 static void linger_cancel(struct ceph_osd_linger_request *lreq)
2947 {
2948         struct ceph_osd_client *osdc = lreq->osdc;
2949
2950         down_write(&osdc->lock);
2951         if (__linger_registered(lreq))
2952                 __linger_cancel(lreq);
2953         up_write(&osdc->lock);
2954 }
2955
2956 static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
2957
2958 static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
2959 {
2960         struct ceph_osd_client *osdc = lreq->osdc;
2961         struct ceph_osdmap *map = osdc->osdmap;
2962
2963         verify_osdc_wrlocked(osdc);
2964         WARN_ON(!map->epoch);
2965
2966         if (lreq->register_gen) {
2967                 lreq->map_dne_bound = map->epoch;
2968                 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
2969                      lreq, lreq->linger_id);
2970         } else {
2971                 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
2972                      __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2973                      map->epoch);
2974         }
2975
2976         if (lreq->map_dne_bound) {
2977                 if (map->epoch >= lreq->map_dne_bound) {
2978                         /* we had a new enough map */
2979                         pr_info("linger_id %llu pool does not exist\n",
2980                                 lreq->linger_id);
2981                         linger_reg_commit_complete(lreq, -ENOENT);
2982                         __linger_cancel(lreq);
2983                 }
2984         } else {
2985                 send_linger_map_check(lreq);
2986         }
2987 }
2988
2989 static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
2990 {
2991         struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2992         struct ceph_osd_linger_request *lreq;
2993         u64 linger_id = greq->private_data;
2994
2995         WARN_ON(greq->result || !greq->u.newest);
2996
2997         down_write(&osdc->lock);
2998         lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
2999         if (!lreq) {
3000                 dout("%s linger_id %llu dne\n", __func__, linger_id);
3001                 goto out_unlock;
3002         }
3003
3004         dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3005              __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3006              greq->u.newest);
3007         if (!lreq->map_dne_bound)
3008                 lreq->map_dne_bound = greq->u.newest;
3009         erase_linger_mc(&osdc->linger_map_checks, lreq);
3010         check_linger_pool_dne(lreq);
3011
3012         linger_put(lreq);
3013 out_unlock:
3014         up_write(&osdc->lock);
3015 }
3016
3017 static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3018 {
3019         struct ceph_osd_client *osdc = lreq->osdc;
3020         struct ceph_osd_linger_request *lookup_lreq;
3021         int ret;
3022
3023         verify_osdc_wrlocked(osdc);
3024
3025         lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3026                                        lreq->linger_id);
3027         if (lookup_lreq) {
3028                 WARN_ON(lookup_lreq != lreq);
3029                 return;
3030         }
3031
3032         linger_get(lreq);
3033         insert_linger_mc(&osdc->linger_map_checks, lreq);
3034         ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3035                                           linger_map_check_cb, lreq->linger_id);
3036         WARN_ON(ret);
3037 }
3038
3039 static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3040 {
3041         int ret;
3042
3043         dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3044         ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3045         return ret ?: lreq->reg_commit_error;
3046 }
3047
3048 static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
3049 {
3050         int ret;
3051
3052         dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3053         ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
3054         return ret ?: lreq->notify_finish_error;
3055 }
3056
3057 /*
3058  * Timeout callback, called every N seconds.  When 1 or more OSD
3059  * requests has been active for more than N seconds, we send a keepalive
3060  * (tag + timestamp) to its OSD to ensure any communications channel
3061  * reset is detected.
3062  */
3063 static void handle_timeout(struct work_struct *work)
3064 {
3065         struct ceph_osd_client *osdc =
3066                 container_of(work, struct ceph_osd_client, timeout_work.work);
3067         struct ceph_options *opts = osdc->client->options;
3068         unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
3069         unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
3070         LIST_HEAD(slow_osds);
3071         struct rb_node *n, *p;
3072
3073         dout("%s osdc %p\n", __func__, osdc);
3074         down_write(&osdc->lock);
3075
3076         /*
3077          * ping osds that are a bit slow.  this ensures that if there
3078          * is a break in the TCP connection we will notice, and reopen
3079          * a connection with that osd (from the fault callback).
3080          */
3081         for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3082                 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3083                 bool found = false;
3084
3085                 for (p = rb_first(&osd->o_requests); p; ) {
3086                         struct ceph_osd_request *req =
3087                             rb_entry(p, struct ceph_osd_request, r_node);
3088
3089                         p = rb_next(p); /* abort_request() */
3090
3091                         if (time_before(req->r_stamp, cutoff)) {
3092                                 dout(" req %p tid %llu on osd%d is laggy\n",
3093                                      req, req->r_tid, osd->o_osd);
3094                                 found = true;
3095                         }
3096                         if (opts->osd_request_timeout &&
3097                             time_before(req->r_start_stamp, expiry_cutoff)) {
3098                                 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3099                                        req->r_tid, osd->o_osd);
3100                                 abort_request(req, -ETIMEDOUT);
3101                         }
3102                 }
3103                 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3104                         struct ceph_osd_linger_request *lreq =
3105                             rb_entry(p, struct ceph_osd_linger_request, node);
3106
3107                         dout(" lreq %p linger_id %llu is served by osd%d\n",
3108                              lreq, lreq->linger_id, osd->o_osd);
3109                         found = true;
3110
3111                         mutex_lock(&lreq->lock);
3112                         if (lreq->is_watch && lreq->committed && !lreq->last_error)
3113                                 send_linger_ping(lreq);
3114                         mutex_unlock(&lreq->lock);
3115                 }
3116
3117                 if (found)
3118                         list_move_tail(&osd->o_keepalive_item, &slow_osds);
3119         }
3120
3121         if (opts->osd_request_timeout) {
3122                 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3123                         struct ceph_osd_request *req =
3124                             rb_entry(p, struct ceph_osd_request, r_node);
3125
3126                         p = rb_next(p); /* abort_request() */
3127
3128                         if (time_before(req->r_start_stamp, expiry_cutoff)) {
3129                                 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3130                                        req->r_tid, osdc->homeless_osd.o_osd);
3131                                 abort_request(req, -ETIMEDOUT);
3132                         }
3133                 }
3134         }
3135
3136         if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3137                 maybe_request_map(osdc);
3138
3139         while (!list_empty(&slow_osds)) {
3140                 struct ceph_osd *osd = list_first_entry(&slow_osds,
3141                                                         struct ceph_osd,
3142                                                         o_keepalive_item);
3143                 list_del_init(&osd->o_keepalive_item);
3144                 ceph_con_keepalive(&osd->o_con);
3145         }
3146
3147         up_write(&osdc->lock);
3148         schedule_delayed_work(&osdc->timeout_work,
3149                               osdc->client->options->osd_keepalive_timeout);
3150 }
3151
3152 static void handle_osds_timeout(struct work_struct *work)
3153 {
3154         struct ceph_osd_client *osdc =
3155                 container_of(work, struct ceph_osd_client,
3156                              osds_timeout_work.work);
3157         unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
3158         struct ceph_osd *osd, *nosd;
3159
3160         dout("%s osdc %p\n", __func__, osdc);
3161         down_write(&osdc->lock);
3162         list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3163                 if (time_before(jiffies, osd->lru_ttl))
3164                         break;
3165
3166                 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3167                 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
3168                 close_osd(osd);
3169         }
3170
3171         up_write(&osdc->lock);
3172         schedule_delayed_work(&osdc->osds_timeout_work,
3173                               round_jiffies_relative(delay));
3174 }
3175
3176 static int ceph_oloc_decode(void **p, void *end,
3177                             struct ceph_object_locator *oloc)
3178 {
3179         u8 struct_v, struct_cv;
3180         u32 len;
3181         void *struct_end;
3182         int ret = 0;
3183
3184         ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3185         struct_v = ceph_decode_8(p);
3186         struct_cv = ceph_decode_8(p);
3187         if (struct_v < 3) {
3188                 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3189                         struct_v, struct_cv);
3190                 goto e_inval;
3191         }
3192         if (struct_cv > 6) {
3193                 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3194                         struct_v, struct_cv);
3195                 goto e_inval;
3196         }
3197         len = ceph_decode_32(p);
3198         ceph_decode_need(p, end, len, e_inval);
3199         struct_end = *p + len;
3200
3201         oloc->pool = ceph_decode_64(p);
3202         *p += 4; /* skip preferred */
3203
3204         len = ceph_decode_32(p);
3205         if (len > 0) {
3206                 pr_warn("ceph_object_locator::key is set\n");
3207                 goto e_inval;
3208         }
3209
3210         if (struct_v >= 5) {
3211                 bool changed = false;
3212
3213                 len = ceph_decode_32(p);
3214                 if (len > 0) {
3215                         ceph_decode_need(p, end, len, e_inval);
3216                         if (!oloc->pool_ns ||
3217                             ceph_compare_string(oloc->pool_ns, *p, len))
3218                                 changed = true;
3219                         *p += len;
3220                 } else {
3221                         if (oloc->pool_ns)
3222                                 changed = true;
3223                 }
3224                 if (changed) {
3225                         /* redirect changes namespace */
3226                         pr_warn("ceph_object_locator::nspace is changed\n");
3227                         goto e_inval;
3228                 }
3229         }
3230
3231         if (struct_v >= 6) {
3232                 s64 hash = ceph_decode_64(p);
3233                 if (hash != -1) {
3234                         pr_warn("ceph_object_locator::hash is set\n");
3235                         goto e_inval;
3236                 }
3237         }
3238
3239         /* skip the rest */
3240         *p = struct_end;
3241 out:
3242         return ret;
3243
3244 e_inval:
3245         ret = -EINVAL;
3246         goto out;
3247 }
3248
3249 static int ceph_redirect_decode(void **p, void *end,
3250                                 struct ceph_request_redirect *redir)
3251 {
3252         u8 struct_v, struct_cv;
3253         u32 len;
3254         void *struct_end;
3255         int ret;
3256
3257         ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3258         struct_v = ceph_decode_8(p);
3259         struct_cv = ceph_decode_8(p);
3260         if (struct_cv > 1) {
3261                 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3262                         struct_v, struct_cv);
3263                 goto e_inval;
3264         }
3265         len = ceph_decode_32(p);
3266         ceph_decode_need(p, end, len, e_inval);
3267         struct_end = *p + len;
3268
3269         ret = ceph_oloc_decode(p, end, &redir->oloc);
3270         if (ret)
3271                 goto out;
3272
3273         len = ceph_decode_32(p);
3274         if (len > 0) {
3275                 pr_warn("ceph_request_redirect::object_name is set\n");
3276                 goto e_inval;
3277         }
3278
3279         len = ceph_decode_32(p);
3280         *p += len; /* skip osd_instructions */
3281
3282         /* skip the rest */
3283         *p = struct_end;
3284 out:
3285         return ret;
3286
3287 e_inval:
3288         ret = -EINVAL;
3289         goto out;
3290 }
3291
3292 struct MOSDOpReply {
3293         struct ceph_pg pgid;
3294         u64 flags;
3295         int result;
3296         u32 epoch;
3297         int num_ops;
3298         u32 outdata_len[CEPH_OSD_MAX_OPS];
3299         s32 rval[CEPH_OSD_MAX_OPS];
3300         int retry_attempt;
3301         struct ceph_eversion replay_version;
3302         u64 user_version;
3303         struct ceph_request_redirect redirect;
3304 };
3305
3306 static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
3307 {
3308         void *p = msg->front.iov_base;
3309         void *const end = p + msg->front.iov_len;
3310         u16 version = le16_to_cpu(msg->hdr.version);
3311         struct ceph_eversion bad_replay_version;
3312         u8 decode_redir;
3313         u32 len;
3314         int ret;
3315         int i;