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