xref: /openbmc/linux/net/ceph/osd_client.c (revision aa26d662)
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 	refcount_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 (refcount_inc_not_zero(&osd->o_ref)) {
1054 		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1055 		     refcount_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, refcount_read(&osd->o_ref),
1066 	     refcount_read(&osd->o_ref) - 1);
1067 	if (refcount_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 
1507 	/* reassert_version */
1508 	memset(p, 0, sizeof(struct ceph_eversion));
1509 	p += sizeof(struct ceph_eversion);
1510 
1511 	/* oloc */
1512 	ceph_start_encoding(&p, 5, 4,
1513 			    ceph_oloc_encoding_size(&req->r_t.target_oloc));
1514 	ceph_encode_64(&p, req->r_t.target_oloc.pool);
1515 	ceph_encode_32(&p, -1); /* preferred */
1516 	ceph_encode_32(&p, 0); /* key len */
1517 	if (req->r_t.target_oloc.pool_ns)
1518 		ceph_encode_string(&p, end, req->r_t.target_oloc.pool_ns->str,
1519 				   req->r_t.target_oloc.pool_ns->len);
1520 	else
1521 		ceph_encode_32(&p, 0);
1522 
1523 	/* pgid */
1524 	ceph_encode_8(&p, 1);
1525 	ceph_encode_64(&p, req->r_t.pgid.pool);
1526 	ceph_encode_32(&p, req->r_t.pgid.seed);
1527 	ceph_encode_32(&p, -1); /* preferred */
1528 
1529 	/* oid */
1530 	ceph_encode_32(&p, req->r_t.target_oid.name_len);
1531 	memcpy(p, req->r_t.target_oid.name, req->r_t.target_oid.name_len);
1532 	p += req->r_t.target_oid.name_len;
1533 
1534 	/* ops, can imply data */
1535 	ceph_encode_16(&p, req->r_num_ops);
1536 	for (i = 0; i < req->r_num_ops; i++) {
1537 		data_len += osd_req_encode_op(p, &req->r_ops[i]);
1538 		p += sizeof(struct ceph_osd_op);
1539 	}
1540 
1541 	ceph_encode_64(&p, req->r_snapid); /* snapid */
1542 	if (req->r_snapc) {
1543 		ceph_encode_64(&p, req->r_snapc->seq);
1544 		ceph_encode_32(&p, req->r_snapc->num_snaps);
1545 		for (i = 0; i < req->r_snapc->num_snaps; i++)
1546 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
1547 	} else {
1548 		ceph_encode_64(&p, 0); /* snap_seq */
1549 		ceph_encode_32(&p, 0); /* snaps len */
1550 	}
1551 
1552 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
1553 
1554 	BUG_ON(p > end);
1555 	msg->front.iov_len = p - msg->front.iov_base;
1556 	msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
1557 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
1558 	msg->hdr.data_len = cpu_to_le32(data_len);
1559 	/*
1560 	 * The header "data_off" is a hint to the receiver allowing it
1561 	 * to align received data into its buffers such that there's no
1562 	 * need to re-copy it before writing it to disk (direct I/O).
1563 	 */
1564 	msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
1565 
1566 	dout("%s req %p oid %s oid_len %d front %zu data %u\n", __func__,
1567 	     req, req->r_t.target_oid.name, req->r_t.target_oid.name_len,
1568 	     msg->front.iov_len, data_len);
1569 }
1570 
1571 /*
1572  * @req has to be assigned a tid and registered.
1573  */
1574 static void send_request(struct ceph_osd_request *req)
1575 {
1576 	struct ceph_osd *osd = req->r_osd;
1577 
1578 	verify_osd_locked(osd);
1579 	WARN_ON(osd->o_osd != req->r_t.osd);
1580 
1581 	/*
1582 	 * We may have a previously queued request message hanging
1583 	 * around.  Cancel it to avoid corrupting the msgr.
1584 	 */
1585 	if (req->r_sent)
1586 		ceph_msg_revoke(req->r_request);
1587 
1588 	req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
1589 	if (req->r_attempts)
1590 		req->r_flags |= CEPH_OSD_FLAG_RETRY;
1591 	else
1592 		WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
1593 
1594 	encode_request(req, req->r_request);
1595 
1596 	dout("%s req %p tid %llu to pg %llu.%x osd%d flags 0x%x attempt %d\n",
1597 	     __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
1598 	     req->r_t.osd, req->r_flags, req->r_attempts);
1599 
1600 	req->r_t.paused = false;
1601 	req->r_stamp = jiffies;
1602 	req->r_attempts++;
1603 
1604 	req->r_sent = osd->o_incarnation;
1605 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
1606 	ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
1607 }
1608 
1609 static void maybe_request_map(struct ceph_osd_client *osdc)
1610 {
1611 	bool continuous = false;
1612 
1613 	verify_osdc_locked(osdc);
1614 	WARN_ON(!osdc->osdmap->epoch);
1615 
1616 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1617 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
1618 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
1619 		dout("%s osdc %p continuous\n", __func__, osdc);
1620 		continuous = true;
1621 	} else {
1622 		dout("%s osdc %p onetime\n", __func__, osdc);
1623 	}
1624 
1625 	if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
1626 			       osdc->osdmap->epoch + 1, continuous))
1627 		ceph_monc_renew_subs(&osdc->client->monc);
1628 }
1629 
1630 static void send_map_check(struct ceph_osd_request *req);
1631 
1632 static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
1633 {
1634 	struct ceph_osd_client *osdc = req->r_osdc;
1635 	struct ceph_osd *osd;
1636 	enum calc_target_result ct_res;
1637 	bool need_send = false;
1638 	bool promoted = false;
1639 
1640 	WARN_ON(req->r_tid);
1641 	dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
1642 
1643 again:
1644 	ct_res = calc_target(osdc, &req->r_t, &req->r_last_force_resend, false);
1645 	if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
1646 		goto promote;
1647 
1648 	osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
1649 	if (IS_ERR(osd)) {
1650 		WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
1651 		goto promote;
1652 	}
1653 
1654 	if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1655 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
1656 		dout("req %p pausewr\n", req);
1657 		req->r_t.paused = true;
1658 		maybe_request_map(osdc);
1659 	} else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1660 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
1661 		dout("req %p pauserd\n", req);
1662 		req->r_t.paused = true;
1663 		maybe_request_map(osdc);
1664 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1665 		   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
1666 				     CEPH_OSD_FLAG_FULL_FORCE)) &&
1667 		   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1668 		    pool_full(osdc, req->r_t.base_oloc.pool))) {
1669 		dout("req %p full/pool_full\n", req);
1670 		pr_warn_ratelimited("FULL or reached pool quota\n");
1671 		req->r_t.paused = true;
1672 		maybe_request_map(osdc);
1673 	} else if (!osd_homeless(osd)) {
1674 		need_send = true;
1675 	} else {
1676 		maybe_request_map(osdc);
1677 	}
1678 
1679 	mutex_lock(&osd->lock);
1680 	/*
1681 	 * Assign the tid atomically with send_request() to protect
1682 	 * multiple writes to the same object from racing with each
1683 	 * other, resulting in out of order ops on the OSDs.
1684 	 */
1685 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
1686 	link_request(osd, req);
1687 	if (need_send)
1688 		send_request(req);
1689 	mutex_unlock(&osd->lock);
1690 
1691 	if (ct_res == CALC_TARGET_POOL_DNE)
1692 		send_map_check(req);
1693 
1694 	if (promoted)
1695 		downgrade_write(&osdc->lock);
1696 	return;
1697 
1698 promote:
1699 	up_read(&osdc->lock);
1700 	down_write(&osdc->lock);
1701 	wrlocked = true;
1702 	promoted = true;
1703 	goto again;
1704 }
1705 
1706 static void account_request(struct ceph_osd_request *req)
1707 {
1708 	WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
1709 	WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
1710 
1711 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
1712 	atomic_inc(&req->r_osdc->num_requests);
1713 
1714 	req->r_start_stamp = jiffies;
1715 }
1716 
1717 static void submit_request(struct ceph_osd_request *req, bool wrlocked)
1718 {
1719 	ceph_osdc_get_request(req);
1720 	account_request(req);
1721 	__submit_request(req, wrlocked);
1722 }
1723 
1724 static void finish_request(struct ceph_osd_request *req)
1725 {
1726 	struct ceph_osd_client *osdc = req->r_osdc;
1727 	struct ceph_osd *osd = req->r_osd;
1728 
1729 	verify_osd_locked(osd);
1730 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1731 
1732 	WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
1733 	unlink_request(osd, req);
1734 	atomic_dec(&osdc->num_requests);
1735 
1736 	/*
1737 	 * If an OSD has failed or returned and a request has been sent
1738 	 * twice, it's possible to get a reply and end up here while the
1739 	 * request message is queued for delivery.  We will ignore the
1740 	 * reply, so not a big deal, but better to try and catch it.
1741 	 */
1742 	ceph_msg_revoke(req->r_request);
1743 	ceph_msg_revoke_incoming(req->r_reply);
1744 }
1745 
1746 static void __complete_request(struct ceph_osd_request *req)
1747 {
1748 	if (req->r_callback) {
1749 		dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
1750 		     req->r_tid, req->r_callback, req->r_result);
1751 		req->r_callback(req);
1752 	}
1753 }
1754 
1755 /*
1756  * This is open-coded in handle_reply().
1757  */
1758 static void complete_request(struct ceph_osd_request *req, int err)
1759 {
1760 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
1761 
1762 	req->r_result = err;
1763 	finish_request(req);
1764 	__complete_request(req);
1765 	complete_all(&req->r_completion);
1766 	ceph_osdc_put_request(req);
1767 }
1768 
1769 static void cancel_map_check(struct ceph_osd_request *req)
1770 {
1771 	struct ceph_osd_client *osdc = req->r_osdc;
1772 	struct ceph_osd_request *lookup_req;
1773 
1774 	verify_osdc_wrlocked(osdc);
1775 
1776 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1777 	if (!lookup_req)
1778 		return;
1779 
1780 	WARN_ON(lookup_req != req);
1781 	erase_request_mc(&osdc->map_checks, req);
1782 	ceph_osdc_put_request(req);
1783 }
1784 
1785 static void cancel_request(struct ceph_osd_request *req)
1786 {
1787 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
1788 
1789 	cancel_map_check(req);
1790 	finish_request(req);
1791 	complete_all(&req->r_completion);
1792 	ceph_osdc_put_request(req);
1793 }
1794 
1795 static void abort_request(struct ceph_osd_request *req, int err)
1796 {
1797 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
1798 
1799 	cancel_map_check(req);
1800 	complete_request(req, err);
1801 }
1802 
1803 static void check_pool_dne(struct ceph_osd_request *req)
1804 {
1805 	struct ceph_osd_client *osdc = req->r_osdc;
1806 	struct ceph_osdmap *map = osdc->osdmap;
1807 
1808 	verify_osdc_wrlocked(osdc);
1809 	WARN_ON(!map->epoch);
1810 
1811 	if (req->r_attempts) {
1812 		/*
1813 		 * We sent a request earlier, which means that
1814 		 * previously the pool existed, and now it does not
1815 		 * (i.e., it was deleted).
1816 		 */
1817 		req->r_map_dne_bound = map->epoch;
1818 		dout("%s req %p tid %llu pool disappeared\n", __func__, req,
1819 		     req->r_tid);
1820 	} else {
1821 		dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
1822 		     req, req->r_tid, req->r_map_dne_bound, map->epoch);
1823 	}
1824 
1825 	if (req->r_map_dne_bound) {
1826 		if (map->epoch >= req->r_map_dne_bound) {
1827 			/* we had a new enough map */
1828 			pr_info_ratelimited("tid %llu pool does not exist\n",
1829 					    req->r_tid);
1830 			complete_request(req, -ENOENT);
1831 		}
1832 	} else {
1833 		send_map_check(req);
1834 	}
1835 }
1836 
1837 static void map_check_cb(struct ceph_mon_generic_request *greq)
1838 {
1839 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
1840 	struct ceph_osd_request *req;
1841 	u64 tid = greq->private_data;
1842 
1843 	WARN_ON(greq->result || !greq->u.newest);
1844 
1845 	down_write(&osdc->lock);
1846 	req = lookup_request_mc(&osdc->map_checks, tid);
1847 	if (!req) {
1848 		dout("%s tid %llu dne\n", __func__, tid);
1849 		goto out_unlock;
1850 	}
1851 
1852 	dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
1853 	     req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
1854 	if (!req->r_map_dne_bound)
1855 		req->r_map_dne_bound = greq->u.newest;
1856 	erase_request_mc(&osdc->map_checks, req);
1857 	check_pool_dne(req);
1858 
1859 	ceph_osdc_put_request(req);
1860 out_unlock:
1861 	up_write(&osdc->lock);
1862 }
1863 
1864 static void send_map_check(struct ceph_osd_request *req)
1865 {
1866 	struct ceph_osd_client *osdc = req->r_osdc;
1867 	struct ceph_osd_request *lookup_req;
1868 	int ret;
1869 
1870 	verify_osdc_wrlocked(osdc);
1871 
1872 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
1873 	if (lookup_req) {
1874 		WARN_ON(lookup_req != req);
1875 		return;
1876 	}
1877 
1878 	ceph_osdc_get_request(req);
1879 	insert_request_mc(&osdc->map_checks, req);
1880 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
1881 					  map_check_cb, req->r_tid);
1882 	WARN_ON(ret);
1883 }
1884 
1885 /*
1886  * lingering requests, watch/notify v2 infrastructure
1887  */
1888 static void linger_release(struct kref *kref)
1889 {
1890 	struct ceph_osd_linger_request *lreq =
1891 	    container_of(kref, struct ceph_osd_linger_request, kref);
1892 
1893 	dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
1894 	     lreq->reg_req, lreq->ping_req);
1895 	WARN_ON(!RB_EMPTY_NODE(&lreq->node));
1896 	WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
1897 	WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
1898 	WARN_ON(!list_empty(&lreq->scan_item));
1899 	WARN_ON(!list_empty(&lreq->pending_lworks));
1900 	WARN_ON(lreq->osd);
1901 
1902 	if (lreq->reg_req)
1903 		ceph_osdc_put_request(lreq->reg_req);
1904 	if (lreq->ping_req)
1905 		ceph_osdc_put_request(lreq->ping_req);
1906 	target_destroy(&lreq->t);
1907 	kfree(lreq);
1908 }
1909 
1910 static void linger_put(struct ceph_osd_linger_request *lreq)
1911 {
1912 	if (lreq)
1913 		kref_put(&lreq->kref, linger_release);
1914 }
1915 
1916 static struct ceph_osd_linger_request *
1917 linger_get(struct ceph_osd_linger_request *lreq)
1918 {
1919 	kref_get(&lreq->kref);
1920 	return lreq;
1921 }
1922 
1923 static struct ceph_osd_linger_request *
1924 linger_alloc(struct ceph_osd_client *osdc)
1925 {
1926 	struct ceph_osd_linger_request *lreq;
1927 
1928 	lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
1929 	if (!lreq)
1930 		return NULL;
1931 
1932 	kref_init(&lreq->kref);
1933 	mutex_init(&lreq->lock);
1934 	RB_CLEAR_NODE(&lreq->node);
1935 	RB_CLEAR_NODE(&lreq->osdc_node);
1936 	RB_CLEAR_NODE(&lreq->mc_node);
1937 	INIT_LIST_HEAD(&lreq->scan_item);
1938 	INIT_LIST_HEAD(&lreq->pending_lworks);
1939 	init_completion(&lreq->reg_commit_wait);
1940 	init_completion(&lreq->notify_finish_wait);
1941 
1942 	lreq->osdc = osdc;
1943 	target_init(&lreq->t);
1944 
1945 	dout("%s lreq %p\n", __func__, lreq);
1946 	return lreq;
1947 }
1948 
1949 DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
1950 DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
1951 DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
1952 
1953 /*
1954  * Create linger request <-> OSD session relation.
1955  *
1956  * @lreq has to be registered, @osd may be homeless.
1957  */
1958 static void link_linger(struct ceph_osd *osd,
1959 			struct ceph_osd_linger_request *lreq)
1960 {
1961 	verify_osd_locked(osd);
1962 	WARN_ON(!lreq->linger_id || lreq->osd);
1963 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
1964 	     osd->o_osd, lreq, lreq->linger_id);
1965 
1966 	if (!osd_homeless(osd))
1967 		__remove_osd_from_lru(osd);
1968 	else
1969 		atomic_inc(&osd->o_osdc->num_homeless);
1970 
1971 	get_osd(osd);
1972 	insert_linger(&osd->o_linger_requests, lreq);
1973 	lreq->osd = osd;
1974 }
1975 
1976 static void unlink_linger(struct ceph_osd *osd,
1977 			  struct ceph_osd_linger_request *lreq)
1978 {
1979 	verify_osd_locked(osd);
1980 	WARN_ON(lreq->osd != osd);
1981 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
1982 	     osd->o_osd, lreq, lreq->linger_id);
1983 
1984 	lreq->osd = NULL;
1985 	erase_linger(&osd->o_linger_requests, lreq);
1986 	put_osd(osd);
1987 
1988 	if (!osd_homeless(osd))
1989 		maybe_move_osd_to_lru(osd);
1990 	else
1991 		atomic_dec(&osd->o_osdc->num_homeless);
1992 }
1993 
1994 static bool __linger_registered(struct ceph_osd_linger_request *lreq)
1995 {
1996 	verify_osdc_locked(lreq->osdc);
1997 
1998 	return !RB_EMPTY_NODE(&lreq->osdc_node);
1999 }
2000 
2001 static bool linger_registered(struct ceph_osd_linger_request *lreq)
2002 {
2003 	struct ceph_osd_client *osdc = lreq->osdc;
2004 	bool registered;
2005 
2006 	down_read(&osdc->lock);
2007 	registered = __linger_registered(lreq);
2008 	up_read(&osdc->lock);
2009 
2010 	return registered;
2011 }
2012 
2013 static void linger_register(struct ceph_osd_linger_request *lreq)
2014 {
2015 	struct ceph_osd_client *osdc = lreq->osdc;
2016 
2017 	verify_osdc_wrlocked(osdc);
2018 	WARN_ON(lreq->linger_id);
2019 
2020 	linger_get(lreq);
2021 	lreq->linger_id = ++osdc->last_linger_id;
2022 	insert_linger_osdc(&osdc->linger_requests, lreq);
2023 }
2024 
2025 static void linger_unregister(struct ceph_osd_linger_request *lreq)
2026 {
2027 	struct ceph_osd_client *osdc = lreq->osdc;
2028 
2029 	verify_osdc_wrlocked(osdc);
2030 
2031 	erase_linger_osdc(&osdc->linger_requests, lreq);
2032 	linger_put(lreq);
2033 }
2034 
2035 static void cancel_linger_request(struct ceph_osd_request *req)
2036 {
2037 	struct ceph_osd_linger_request *lreq = req->r_priv;
2038 
2039 	WARN_ON(!req->r_linger);
2040 	cancel_request(req);
2041 	linger_put(lreq);
2042 }
2043 
2044 struct linger_work {
2045 	struct work_struct work;
2046 	struct ceph_osd_linger_request *lreq;
2047 	struct list_head pending_item;
2048 	unsigned long queued_stamp;
2049 
2050 	union {
2051 		struct {
2052 			u64 notify_id;
2053 			u64 notifier_id;
2054 			void *payload; /* points into @msg front */
2055 			size_t payload_len;
2056 
2057 			struct ceph_msg *msg; /* for ceph_msg_put() */
2058 		} notify;
2059 		struct {
2060 			int err;
2061 		} error;
2062 	};
2063 };
2064 
2065 static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2066 				       work_func_t workfn)
2067 {
2068 	struct linger_work *lwork;
2069 
2070 	lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2071 	if (!lwork)
2072 		return NULL;
2073 
2074 	INIT_WORK(&lwork->work, workfn);
2075 	INIT_LIST_HEAD(&lwork->pending_item);
2076 	lwork->lreq = linger_get(lreq);
2077 
2078 	return lwork;
2079 }
2080 
2081 static void lwork_free(struct linger_work *lwork)
2082 {
2083 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2084 
2085 	mutex_lock(&lreq->lock);
2086 	list_del(&lwork->pending_item);
2087 	mutex_unlock(&lreq->lock);
2088 
2089 	linger_put(lreq);
2090 	kfree(lwork);
2091 }
2092 
2093 static void lwork_queue(struct linger_work *lwork)
2094 {
2095 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2096 	struct ceph_osd_client *osdc = lreq->osdc;
2097 
2098 	verify_lreq_locked(lreq);
2099 	WARN_ON(!list_empty(&lwork->pending_item));
2100 
2101 	lwork->queued_stamp = jiffies;
2102 	list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2103 	queue_work(osdc->notify_wq, &lwork->work);
2104 }
2105 
2106 static void do_watch_notify(struct work_struct *w)
2107 {
2108 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2109 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2110 
2111 	if (!linger_registered(lreq)) {
2112 		dout("%s lreq %p not registered\n", __func__, lreq);
2113 		goto out;
2114 	}
2115 
2116 	WARN_ON(!lreq->is_watch);
2117 	dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2118 	     __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2119 	     lwork->notify.payload_len);
2120 	lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2121 		  lwork->notify.notifier_id, lwork->notify.payload,
2122 		  lwork->notify.payload_len);
2123 
2124 out:
2125 	ceph_msg_put(lwork->notify.msg);
2126 	lwork_free(lwork);
2127 }
2128 
2129 static void do_watch_error(struct work_struct *w)
2130 {
2131 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2132 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2133 
2134 	if (!linger_registered(lreq)) {
2135 		dout("%s lreq %p not registered\n", __func__, lreq);
2136 		goto out;
2137 	}
2138 
2139 	dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2140 	lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2141 
2142 out:
2143 	lwork_free(lwork);
2144 }
2145 
2146 static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2147 {
2148 	struct linger_work *lwork;
2149 
2150 	lwork = lwork_alloc(lreq, do_watch_error);
2151 	if (!lwork) {
2152 		pr_err("failed to allocate error-lwork\n");
2153 		return;
2154 	}
2155 
2156 	lwork->error.err = lreq->last_error;
2157 	lwork_queue(lwork);
2158 }
2159 
2160 static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2161 				       int result)
2162 {
2163 	if (!completion_done(&lreq->reg_commit_wait)) {
2164 		lreq->reg_commit_error = (result <= 0 ? result : 0);
2165 		complete_all(&lreq->reg_commit_wait);
2166 	}
2167 }
2168 
2169 static void linger_commit_cb(struct ceph_osd_request *req)
2170 {
2171 	struct ceph_osd_linger_request *lreq = req->r_priv;
2172 
2173 	mutex_lock(&lreq->lock);
2174 	dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2175 	     lreq->linger_id, req->r_result);
2176 	linger_reg_commit_complete(lreq, req->r_result);
2177 	lreq->committed = true;
2178 
2179 	if (!lreq->is_watch) {
2180 		struct ceph_osd_data *osd_data =
2181 		    osd_req_op_data(req, 0, notify, response_data);
2182 		void *p = page_address(osd_data->pages[0]);
2183 
2184 		WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2185 			osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2186 
2187 		/* make note of the notify_id */
2188 		if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2189 			lreq->notify_id = ceph_decode_64(&p);
2190 			dout("lreq %p notify_id %llu\n", lreq,
2191 			     lreq->notify_id);
2192 		} else {
2193 			dout("lreq %p no notify_id\n", lreq);
2194 		}
2195 	}
2196 
2197 	mutex_unlock(&lreq->lock);
2198 	linger_put(lreq);
2199 }
2200 
2201 static int normalize_watch_error(int err)
2202 {
2203 	/*
2204 	 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2205 	 * notification and a failure to reconnect because we raced with
2206 	 * the delete appear the same to the user.
2207 	 */
2208 	if (err == -ENOENT)
2209 		err = -ENOTCONN;
2210 
2211 	return err;
2212 }
2213 
2214 static void linger_reconnect_cb(struct ceph_osd_request *req)
2215 {
2216 	struct ceph_osd_linger_request *lreq = req->r_priv;
2217 
2218 	mutex_lock(&lreq->lock);
2219 	dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2220 	     lreq, lreq->linger_id, req->r_result, lreq->last_error);
2221 	if (req->r_result < 0) {
2222 		if (!lreq->last_error) {
2223 			lreq->last_error = normalize_watch_error(req->r_result);
2224 			queue_watch_error(lreq);
2225 		}
2226 	}
2227 
2228 	mutex_unlock(&lreq->lock);
2229 	linger_put(lreq);
2230 }
2231 
2232 static void send_linger(struct ceph_osd_linger_request *lreq)
2233 {
2234 	struct ceph_osd_request *req = lreq->reg_req;
2235 	struct ceph_osd_req_op *op = &req->r_ops[0];
2236 
2237 	verify_osdc_wrlocked(req->r_osdc);
2238 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2239 
2240 	if (req->r_osd)
2241 		cancel_linger_request(req);
2242 
2243 	request_reinit(req);
2244 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2245 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2246 	req->r_flags = lreq->t.flags;
2247 	req->r_mtime = lreq->mtime;
2248 
2249 	mutex_lock(&lreq->lock);
2250 	if (lreq->is_watch && lreq->committed) {
2251 		WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2252 			op->watch.cookie != lreq->linger_id);
2253 		op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2254 		op->watch.gen = ++lreq->register_gen;
2255 		dout("lreq %p reconnect register_gen %u\n", lreq,
2256 		     op->watch.gen);
2257 		req->r_callback = linger_reconnect_cb;
2258 	} else {
2259 		if (!lreq->is_watch)
2260 			lreq->notify_id = 0;
2261 		else
2262 			WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2263 		dout("lreq %p register\n", lreq);
2264 		req->r_callback = linger_commit_cb;
2265 	}
2266 	mutex_unlock(&lreq->lock);
2267 
2268 	req->r_priv = linger_get(lreq);
2269 	req->r_linger = true;
2270 
2271 	submit_request(req, true);
2272 }
2273 
2274 static void linger_ping_cb(struct ceph_osd_request *req)
2275 {
2276 	struct ceph_osd_linger_request *lreq = req->r_priv;
2277 
2278 	mutex_lock(&lreq->lock);
2279 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2280 	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2281 	     lreq->last_error);
2282 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
2283 		if (!req->r_result) {
2284 			lreq->watch_valid_thru = lreq->ping_sent;
2285 		} else if (!lreq->last_error) {
2286 			lreq->last_error = normalize_watch_error(req->r_result);
2287 			queue_watch_error(lreq);
2288 		}
2289 	} else {
2290 		dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2291 		     lreq->register_gen, req->r_ops[0].watch.gen);
2292 	}
2293 
2294 	mutex_unlock(&lreq->lock);
2295 	linger_put(lreq);
2296 }
2297 
2298 static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2299 {
2300 	struct ceph_osd_client *osdc = lreq->osdc;
2301 	struct ceph_osd_request *req = lreq->ping_req;
2302 	struct ceph_osd_req_op *op = &req->r_ops[0];
2303 
2304 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2305 		dout("%s PAUSERD\n", __func__);
2306 		return;
2307 	}
2308 
2309 	lreq->ping_sent = jiffies;
2310 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2311 	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
2312 	     lreq->register_gen);
2313 
2314 	if (req->r_osd)
2315 		cancel_linger_request(req);
2316 
2317 	request_reinit(req);
2318 	target_copy(&req->r_t, &lreq->t);
2319 
2320 	WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2321 		op->watch.cookie != lreq->linger_id ||
2322 		op->watch.op != CEPH_OSD_WATCH_OP_PING);
2323 	op->watch.gen = lreq->register_gen;
2324 	req->r_callback = linger_ping_cb;
2325 	req->r_priv = linger_get(lreq);
2326 	req->r_linger = true;
2327 
2328 	ceph_osdc_get_request(req);
2329 	account_request(req);
2330 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
2331 	link_request(lreq->osd, req);
2332 	send_request(req);
2333 }
2334 
2335 static void linger_submit(struct ceph_osd_linger_request *lreq)
2336 {
2337 	struct ceph_osd_client *osdc = lreq->osdc;
2338 	struct ceph_osd *osd;
2339 
2340 	calc_target(osdc, &lreq->t, &lreq->last_force_resend, false);
2341 	osd = lookup_create_osd(osdc, lreq->t.osd, true);
2342 	link_linger(osd, lreq);
2343 
2344 	send_linger(lreq);
2345 }
2346 
2347 static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
2348 {
2349 	struct ceph_osd_client *osdc = lreq->osdc;
2350 	struct ceph_osd_linger_request *lookup_lreq;
2351 
2352 	verify_osdc_wrlocked(osdc);
2353 
2354 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2355 				       lreq->linger_id);
2356 	if (!lookup_lreq)
2357 		return;
2358 
2359 	WARN_ON(lookup_lreq != lreq);
2360 	erase_linger_mc(&osdc->linger_map_checks, lreq);
2361 	linger_put(lreq);
2362 }
2363 
2364 /*
2365  * @lreq has to be both registered and linked.
2366  */
2367 static void __linger_cancel(struct ceph_osd_linger_request *lreq)
2368 {
2369 	if (lreq->is_watch && lreq->ping_req->r_osd)
2370 		cancel_linger_request(lreq->ping_req);
2371 	if (lreq->reg_req->r_osd)
2372 		cancel_linger_request(lreq->reg_req);
2373 	cancel_linger_map_check(lreq);
2374 	unlink_linger(lreq->osd, lreq);
2375 	linger_unregister(lreq);
2376 }
2377 
2378 static void linger_cancel(struct ceph_osd_linger_request *lreq)
2379 {
2380 	struct ceph_osd_client *osdc = lreq->osdc;
2381 
2382 	down_write(&osdc->lock);
2383 	if (__linger_registered(lreq))
2384 		__linger_cancel(lreq);
2385 	up_write(&osdc->lock);
2386 }
2387 
2388 static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
2389 
2390 static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
2391 {
2392 	struct ceph_osd_client *osdc = lreq->osdc;
2393 	struct ceph_osdmap *map = osdc->osdmap;
2394 
2395 	verify_osdc_wrlocked(osdc);
2396 	WARN_ON(!map->epoch);
2397 
2398 	if (lreq->register_gen) {
2399 		lreq->map_dne_bound = map->epoch;
2400 		dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
2401 		     lreq, lreq->linger_id);
2402 	} else {
2403 		dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
2404 		     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2405 		     map->epoch);
2406 	}
2407 
2408 	if (lreq->map_dne_bound) {
2409 		if (map->epoch >= lreq->map_dne_bound) {
2410 			/* we had a new enough map */
2411 			pr_info("linger_id %llu pool does not exist\n",
2412 				lreq->linger_id);
2413 			linger_reg_commit_complete(lreq, -ENOENT);
2414 			__linger_cancel(lreq);
2415 		}
2416 	} else {
2417 		send_linger_map_check(lreq);
2418 	}
2419 }
2420 
2421 static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
2422 {
2423 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2424 	struct ceph_osd_linger_request *lreq;
2425 	u64 linger_id = greq->private_data;
2426 
2427 	WARN_ON(greq->result || !greq->u.newest);
2428 
2429 	down_write(&osdc->lock);
2430 	lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
2431 	if (!lreq) {
2432 		dout("%s linger_id %llu dne\n", __func__, linger_id);
2433 		goto out_unlock;
2434 	}
2435 
2436 	dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
2437 	     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
2438 	     greq->u.newest);
2439 	if (!lreq->map_dne_bound)
2440 		lreq->map_dne_bound = greq->u.newest;
2441 	erase_linger_mc(&osdc->linger_map_checks, lreq);
2442 	check_linger_pool_dne(lreq);
2443 
2444 	linger_put(lreq);
2445 out_unlock:
2446 	up_write(&osdc->lock);
2447 }
2448 
2449 static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
2450 {
2451 	struct ceph_osd_client *osdc = lreq->osdc;
2452 	struct ceph_osd_linger_request *lookup_lreq;
2453 	int ret;
2454 
2455 	verify_osdc_wrlocked(osdc);
2456 
2457 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
2458 				       lreq->linger_id);
2459 	if (lookup_lreq) {
2460 		WARN_ON(lookup_lreq != lreq);
2461 		return;
2462 	}
2463 
2464 	linger_get(lreq);
2465 	insert_linger_mc(&osdc->linger_map_checks, lreq);
2466 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2467 					  linger_map_check_cb, lreq->linger_id);
2468 	WARN_ON(ret);
2469 }
2470 
2471 static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
2472 {
2473 	int ret;
2474 
2475 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2476 	ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
2477 	return ret ?: lreq->reg_commit_error;
2478 }
2479 
2480 static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
2481 {
2482 	int ret;
2483 
2484 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2485 	ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
2486 	return ret ?: lreq->notify_finish_error;
2487 }
2488 
2489 /*
2490  * Timeout callback, called every N seconds.  When 1 or more OSD
2491  * requests has been active for more than N seconds, we send a keepalive
2492  * (tag + timestamp) to its OSD to ensure any communications channel
2493  * reset is detected.
2494  */
2495 static void handle_timeout(struct work_struct *work)
2496 {
2497 	struct ceph_osd_client *osdc =
2498 		container_of(work, struct ceph_osd_client, timeout_work.work);
2499 	struct ceph_options *opts = osdc->client->options;
2500 	unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
2501 	unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
2502 	LIST_HEAD(slow_osds);
2503 	struct rb_node *n, *p;
2504 
2505 	dout("%s osdc %p\n", __func__, osdc);
2506 	down_write(&osdc->lock);
2507 
2508 	/*
2509 	 * ping osds that are a bit slow.  this ensures that if there
2510 	 * is a break in the TCP connection we will notice, and reopen
2511 	 * a connection with that osd (from the fault callback).
2512 	 */
2513 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
2514 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
2515 		bool found = false;
2516 
2517 		for (p = rb_first(&osd->o_requests); p; ) {
2518 			struct ceph_osd_request *req =
2519 			    rb_entry(p, struct ceph_osd_request, r_node);
2520 
2521 			p = rb_next(p); /* abort_request() */
2522 
2523 			if (time_before(req->r_stamp, cutoff)) {
2524 				dout(" req %p tid %llu on osd%d is laggy\n",
2525 				     req, req->r_tid, osd->o_osd);
2526 				found = true;
2527 			}
2528 			if (opts->osd_request_timeout &&
2529 			    time_before(req->r_start_stamp, expiry_cutoff)) {
2530 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
2531 				       req->r_tid, osd->o_osd);
2532 				abort_request(req, -ETIMEDOUT);
2533 			}
2534 		}
2535 		for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
2536 			struct ceph_osd_linger_request *lreq =
2537 			    rb_entry(p, struct ceph_osd_linger_request, node);
2538 
2539 			dout(" lreq %p linger_id %llu is served by osd%d\n",
2540 			     lreq, lreq->linger_id, osd->o_osd);
2541 			found = true;
2542 
2543 			mutex_lock(&lreq->lock);
2544 			if (lreq->is_watch && lreq->committed && !lreq->last_error)
2545 				send_linger_ping(lreq);
2546 			mutex_unlock(&lreq->lock);
2547 		}
2548 
2549 		if (found)
2550 			list_move_tail(&osd->o_keepalive_item, &slow_osds);
2551 	}
2552 
2553 	if (opts->osd_request_timeout) {
2554 		for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
2555 			struct ceph_osd_request *req =
2556 			    rb_entry(p, struct ceph_osd_request, r_node);
2557 
2558 			p = rb_next(p); /* abort_request() */
2559 
2560 			if (time_before(req->r_start_stamp, expiry_cutoff)) {
2561 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
2562 				       req->r_tid, osdc->homeless_osd.o_osd);
2563 				abort_request(req, -ETIMEDOUT);
2564 			}
2565 		}
2566 	}
2567 
2568 	if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
2569 		maybe_request_map(osdc);
2570 
2571 	while (!list_empty(&slow_osds)) {
2572 		struct ceph_osd *osd = list_first_entry(&slow_osds,
2573 							struct ceph_osd,
2574 							o_keepalive_item);
2575 		list_del_init(&osd->o_keepalive_item);
2576 		ceph_con_keepalive(&osd->o_con);
2577 	}
2578 
2579 	up_write(&osdc->lock);
2580 	schedule_delayed_work(&osdc->timeout_work,
2581 			      osdc->client->options->osd_keepalive_timeout);
2582 }
2583 
2584 static void handle_osds_timeout(struct work_struct *work)
2585 {
2586 	struct ceph_osd_client *osdc =
2587 		container_of(work, struct ceph_osd_client,
2588 			     osds_timeout_work.work);
2589 	unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
2590 	struct ceph_osd *osd, *nosd;
2591 
2592 	dout("%s osdc %p\n", __func__, osdc);
2593 	down_write(&osdc->lock);
2594 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
2595 		if (time_before(jiffies, osd->lru_ttl))
2596 			break;
2597 
2598 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
2599 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
2600 		close_osd(osd);
2601 	}
2602 
2603 	up_write(&osdc->lock);
2604 	schedule_delayed_work(&osdc->osds_timeout_work,
2605 			      round_jiffies_relative(delay));
2606 }
2607 
2608 static int ceph_oloc_decode(void **p, void *end,
2609 			    struct ceph_object_locator *oloc)
2610 {
2611 	u8 struct_v, struct_cv;
2612 	u32 len;
2613 	void *struct_end;
2614 	int ret = 0;
2615 
2616 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2617 	struct_v = ceph_decode_8(p);
2618 	struct_cv = ceph_decode_8(p);
2619 	if (struct_v < 3) {
2620 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
2621 			struct_v, struct_cv);
2622 		goto e_inval;
2623 	}
2624 	if (struct_cv > 6) {
2625 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
2626 			struct_v, struct_cv);
2627 		goto e_inval;
2628 	}
2629 	len = ceph_decode_32(p);
2630 	ceph_decode_need(p, end, len, e_inval);
2631 	struct_end = *p + len;
2632 
2633 	oloc->pool = ceph_decode_64(p);
2634 	*p += 4; /* skip preferred */
2635 
2636 	len = ceph_decode_32(p);
2637 	if (len > 0) {
2638 		pr_warn("ceph_object_locator::key is set\n");
2639 		goto e_inval;
2640 	}
2641 
2642 	if (struct_v >= 5) {
2643 		bool changed = false;
2644 
2645 		len = ceph_decode_32(p);
2646 		if (len > 0) {
2647 			ceph_decode_need(p, end, len, e_inval);
2648 			if (!oloc->pool_ns ||
2649 			    ceph_compare_string(oloc->pool_ns, *p, len))
2650 				changed = true;
2651 			*p += len;
2652 		} else {
2653 			if (oloc->pool_ns)
2654 				changed = true;
2655 		}
2656 		if (changed) {
2657 			/* redirect changes namespace */
2658 			pr_warn("ceph_object_locator::nspace is changed\n");
2659 			goto e_inval;
2660 		}
2661 	}
2662 
2663 	if (struct_v >= 6) {
2664 		s64 hash = ceph_decode_64(p);
2665 		if (hash != -1) {
2666 			pr_warn("ceph_object_locator::hash is set\n");
2667 			goto e_inval;
2668 		}
2669 	}
2670 
2671 	/* skip the rest */
2672 	*p = struct_end;
2673 out:
2674 	return ret;
2675 
2676 e_inval:
2677 	ret = -EINVAL;
2678 	goto out;
2679 }
2680 
2681 static int ceph_redirect_decode(void **p, void *end,
2682 				struct ceph_request_redirect *redir)
2683 {
2684 	u8 struct_v, struct_cv;
2685 	u32 len;
2686 	void *struct_end;
2687 	int ret;
2688 
2689 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
2690 	struct_v = ceph_decode_8(p);
2691 	struct_cv = ceph_decode_8(p);
2692 	if (struct_cv > 1) {
2693 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
2694 			struct_v, struct_cv);
2695 		goto e_inval;
2696 	}
2697 	len = ceph_decode_32(p);
2698 	ceph_decode_need(p, end, len, e_inval);
2699 	struct_end = *p + len;
2700 
2701 	ret = ceph_oloc_decode(p, end, &redir->oloc);
2702 	if (ret)
2703 		goto out;
2704 
2705 	len = ceph_decode_32(p);
2706 	if (len > 0) {
2707 		pr_warn("ceph_request_redirect::object_name is set\n");
2708 		goto e_inval;
2709 	}
2710 
2711 	len = ceph_decode_32(p);
2712 	*p += len; /* skip osd_instructions */
2713 
2714 	/* skip the rest */
2715 	*p = struct_end;
2716 out:
2717 	return ret;
2718 
2719 e_inval:
2720 	ret = -EINVAL;
2721 	goto out;
2722 }
2723 
2724 struct MOSDOpReply {
2725 	struct ceph_pg pgid;
2726 	u64 flags;
2727 	int result;
2728 	u32 epoch;
2729 	int num_ops;
2730 	u32 outdata_len[CEPH_OSD_MAX_OPS];
2731 	s32 rval[CEPH_OSD_MAX_OPS];
2732 	int retry_attempt;
2733 	struct ceph_eversion replay_version;
2734 	u64 user_version;
2735 	struct ceph_request_redirect redirect;
2736 };
2737 
2738 static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
2739 {
2740 	void *p = msg->front.iov_base;
2741 	void *const end = p + msg->front.iov_len;
2742 	u16 version = le16_to_cpu(msg->hdr.version);
2743 	struct ceph_eversion bad_replay_version;
2744 	u8 decode_redir;
2745 	u32 len;
2746 	int ret;
2747 	int i;
2748 
2749 	ceph_decode_32_safe(&p, end, len, e_inval);
2750 	ceph_decode_need(&p, end, len, e_inval);
2751 	p += len; /* skip oid */
2752 
2753 	ret = ceph_decode_pgid(&p, end, &m->pgid);
2754 	if (ret)
2755 		return ret;
2756 
2757 	ceph_decode_64_safe(&p, end, m->flags, e_inval);
2758 	ceph_decode_32_safe(&p, end, m->result, e_inval);
2759 	ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
2760 	memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
2761 	p += sizeof(bad_replay_version);
2762 	ceph_decode_32_safe(&p, end, m->epoch, e_inval);
2763 
2764 	ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
2765 	if (m->num_ops > ARRAY_SIZE(m->outdata_len))
2766 		goto e_inval;
2767 
2768 	ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
2769 			 e_inval);
2770 	for (i = 0; i < m->num_ops; i++) {
2771 		struct ceph_osd_op *op = p;
2772 
2773 		m->outdata_len[i] = le32_to_cpu(op->payload_len);
2774 		p += sizeof(*op);
2775 	}
2776 
2777 	ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
2778 	for (i = 0; i < m->num_ops; i++)
2779 		ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
2780 
2781 	if (version >= 5) {
2782 		ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
2783 		memcpy(&m->replay_version, p, sizeof(m->replay_version));
2784 		p += sizeof(m->replay_version);
2785 		ceph_decode_64_safe(&p, end, m->user_version, e_inval);
2786 	} else {
2787 		m->replay_version = bad_replay_version; /* struct */
2788 		m->user_version = le64_to_cpu(m->replay_version.version);
2789 	}
2790 
2791 	if (version >= 6) {
2792 		if (version >= 7)
2793 			ceph_decode_8_safe(&p, end, decode_redir, e_inval);
2794 		else
2795 			decode_redir = 1;
2796 	} else {
2797 		decode_redir = 0;
2798 	}
2799 
2800 	if (decode_redir) {
2801 		ret = ceph_redirect_decode(&p, end, &m->redirect);
2802 		if (ret)
2803 			return ret;
2804 	} else {
2805 		ceph_oloc_init(&m->redirect.oloc);
2806 	}
2807 
2808 	return 0;
2809 
2810 e_inval:
2811 	return -EINVAL;
2812 }
2813 
2814 /*
2815  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
2816  * specified.
2817  */
2818 static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
2819 {
2820 	struct ceph_osd_client *osdc = osd->o_osdc;
2821 	struct ceph_osd_request *req;
2822 	struct MOSDOpReply m;
2823 	u64 tid = le64_to_cpu(msg->hdr.tid);
2824 	u32 data_len = 0;
2825 	int ret;
2826 	int i;
2827 
2828 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
2829 
2830 	down_read(&osdc->lock);
2831 	if (!osd_registered(osd)) {
2832 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
2833 		goto out_unlock_osdc;
2834 	}
2835 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
2836 
2837 	mutex_lock(&osd->lock);
2838 	req = lookup_request(&osd->o_requests, tid);
2839 	if (!req) {
2840 		dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
2841 		goto out_unlock_session;
2842 	}
2843 
2844 	m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
2845 	ret = decode_MOSDOpReply(msg, &m);
2846 	m.redirect.oloc.pool_ns = NULL;
2847 	if (ret) {
2848 		pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
2849 		       req->r_tid, ret);
2850 		ceph_msg_dump(msg);
2851 		goto fail_request;
2852 	}
2853 	dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
2854 	     __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
2855 	     m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
2856 	     le64_to_cpu(m.replay_version.version), m.user_version);
2857 
2858 	if (m.retry_attempt >= 0) {
2859 		if (m.retry_attempt != req->r_attempts - 1) {
2860 			dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
2861 			     req, req->r_tid, m.retry_attempt,
2862 			     req->r_attempts - 1);
2863 			goto out_unlock_session;
2864 		}
2865 	} else {
2866 		WARN_ON(1); /* MOSDOpReply v4 is assumed */
2867 	}
2868 
2869 	if (!ceph_oloc_empty(&m.redirect.oloc)) {
2870 		dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
2871 		     m.redirect.oloc.pool);
2872 		unlink_request(osd, req);
2873 		mutex_unlock(&osd->lock);
2874 
2875 		/*
2876 		 * Not ceph_oloc_copy() - changing pool_ns is not
2877 		 * supported.
2878 		 */
2879 		req->r_t.target_oloc.pool = m.redirect.oloc.pool;
2880 		req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
2881 		req->r_tid = 0;
2882 		__submit_request(req, false);
2883 		goto out_unlock_osdc;
2884 	}
2885 
2886 	if (m.num_ops != req->r_num_ops) {
2887 		pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
2888 		       req->r_num_ops, req->r_tid);
2889 		goto fail_request;
2890 	}
2891 	for (i = 0; i < req->r_num_ops; i++) {
2892 		dout(" req %p tid %llu op %d rval %d len %u\n", req,
2893 		     req->r_tid, i, m.rval[i], m.outdata_len[i]);
2894 		req->r_ops[i].rval = m.rval[i];
2895 		req->r_ops[i].outdata_len = m.outdata_len[i];
2896 		data_len += m.outdata_len[i];
2897 	}
2898 	if (data_len != le32_to_cpu(msg->hdr.data_len)) {
2899 		pr_err("sum of lens %u != %u for tid %llu\n", data_len,
2900 		       le32_to_cpu(msg->hdr.data_len), req->r_tid);
2901 		goto fail_request;
2902 	}
2903 	dout("%s req %p tid %llu result %d data_len %u\n", __func__,
2904 	     req, req->r_tid, m.result, data_len);
2905 
2906 	/*
2907 	 * Since we only ever request ONDISK, we should only ever get
2908 	 * one (type of) reply back.
2909 	 */
2910 	WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
2911 	req->r_result = m.result ?: data_len;
2912 	finish_request(req);
2913 	mutex_unlock(&osd->lock);
2914 	up_read(&osdc->lock);
2915 
2916 	__complete_request(req);
2917 	complete_all(&req->r_completion);
2918 	ceph_osdc_put_request(req);
2919 	return;
2920 
2921 fail_request:
2922 	complete_request(req, -EIO);
2923 out_unlock_session:
2924 	mutex_unlock(&osd->lock);
2925 out_unlock_osdc:
2926 	up_read(&osdc->lock);
2927 }
2928 
2929 static void set_pool_was_full(struct ceph_osd_client *osdc)
2930 {
2931 	struct rb_node *n;
2932 
2933 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
2934 		struct ceph_pg_pool_info *pi =
2935 		    rb_entry(n, struct ceph_pg_pool_info, node);
2936 
2937 		pi->was_full = __pool_full(pi);
2938 	}
2939 }
2940 
2941 static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
2942 {
2943 	struct ceph_pg_pool_info *pi;
2944 
2945 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
2946 	if (!pi)
2947 		return false;
2948 
2949 	return pi->was_full && !__pool_full(pi);
2950 }
2951 
2952 static enum calc_target_result
2953 recalc_linger_target(struct ceph_osd_linger_request *lreq)
2954 {
2955 	struct ceph_osd_client *osdc = lreq->osdc;
2956 	enum calc_target_result ct_res;
2957 
2958 	ct_res = calc_target(osdc, &lreq->t, &lreq->last_force_resend, true);
2959 	if (ct_res == CALC_TARGET_NEED_RESEND) {
2960 		struct ceph_osd *osd;
2961 
2962 		osd = lookup_create_osd(osdc, lreq->t.osd, true);
2963 		if (osd != lreq->osd) {
2964 			unlink_linger(lreq->osd, lreq);
2965 			link_linger(osd, lreq);
2966 		}
2967 	}
2968 
2969 	return ct_res;
2970 }
2971 
2972 /*
2973  * Requeue requests whose mapping to an OSD has changed.
2974  */
2975 static void scan_requests(struct ceph_osd *osd,
2976 			  bool force_resend,
2977 			  bool cleared_full,
2978 			  bool check_pool_cleared_full,
2979 			  struct rb_root *need_resend,
2980 			  struct list_head *need_resend_linger)
2981 {
2982 	struct ceph_osd_client *osdc = osd->o_osdc;
2983 	struct rb_node *n;
2984 	bool force_resend_writes;
2985 
2986 	for (n = rb_first(&osd->o_linger_requests); n; ) {
2987 		struct ceph_osd_linger_request *lreq =
2988 		    rb_entry(n, struct ceph_osd_linger_request, node);
2989 		enum calc_target_result ct_res;
2990 
2991 		n = rb_next(n); /* recalc_linger_target() */
2992 
2993 		dout("%s lreq %p linger_id %llu\n", __func__, lreq,
2994 		     lreq->linger_id);
2995 		ct_res = recalc_linger_target(lreq);
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, lreq->t.base_oloc.pool));
3001 			if (!force_resend && !force_resend_writes)
3002 				break;
3003 
3004 			/* fall through */
3005 		case CALC_TARGET_NEED_RESEND:
3006 			cancel_linger_map_check(lreq);
3007 			/*
3008 			 * scan_requests() for the previous epoch(s)
3009 			 * may have already added it to the list, since
3010 			 * it's not unlinked here.
3011 			 */
3012 			if (list_empty(&lreq->scan_item))
3013 				list_add_tail(&lreq->scan_item, need_resend_linger);
3014 			break;
3015 		case CALC_TARGET_POOL_DNE:
3016 			check_linger_pool_dne(lreq);
3017 			break;
3018 		}
3019 	}
3020 
3021 	for (n = rb_first(&osd->o_requests); n; ) {
3022 		struct ceph_osd_request *req =
3023 		    rb_entry(n, struct ceph_osd_request, r_node);
3024 		enum calc_target_result ct_res;
3025 
3026 		n = rb_next(n); /* unlink_request(), check_pool_dne() */
3027 
3028 		dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3029 		ct_res = calc_target(osdc, &req->r_t,
3030 				     &req->r_last_force_resend, false);
3031 		switch (ct_res) {
3032 		case CALC_TARGET_NO_ACTION:
3033 			force_resend_writes = cleared_full ||
3034 			    (check_pool_cleared_full &&
3035 			     pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3036 			if (!force_resend &&
3037 			    (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3038 			     !force_resend_writes))
3039 				break;
3040 
3041 			/* fall through */
3042 		case CALC_TARGET_NEED_RESEND:
3043 			cancel_map_check(req);
3044 			unlink_request(osd, req);
3045 			insert_request(need_resend, req);
3046 			break;
3047 		case CALC_TARGET_POOL_DNE:
3048 			check_pool_dne(req);
3049 			break;
3050 		}
3051 	}
3052 }
3053 
3054 static int handle_one_map(struct ceph_osd_client *osdc,
3055 			  void *p, void *end, bool incremental,
3056 			  struct rb_root *need_resend,
3057 			  struct list_head *need_resend_linger)
3058 {
3059 	struct ceph_osdmap *newmap;
3060 	struct rb_node *n;
3061 	bool skipped_map = false;
3062 	bool was_full;
3063 
3064 	was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3065 	set_pool_was_full(osdc);
3066 
3067 	if (incremental)
3068 		newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3069 	else
3070 		newmap = ceph_osdmap_decode(&p, end);
3071 	if (IS_ERR(newmap))
3072 		return PTR_ERR(newmap);
3073 
3074 	if (newmap != osdc->osdmap) {
3075 		/*
3076 		 * Preserve ->was_full before destroying the old map.
3077 		 * For pools that weren't in the old map, ->was_full
3078 		 * should be false.
3079 		 */
3080 		for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3081 			struct ceph_pg_pool_info *pi =
3082 			    rb_entry(n, struct ceph_pg_pool_info, node);
3083 			struct ceph_pg_pool_info *old_pi;
3084 
3085 			old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3086 			if (old_pi)
3087 				pi->was_full = old_pi->was_full;
3088 			else
3089 				WARN_ON(pi->was_full);
3090 		}
3091 
3092 		if (osdc->osdmap->epoch &&
3093 		    osdc->osdmap->epoch + 1 < newmap->epoch) {
3094 			WARN_ON(incremental);
3095 			skipped_map = true;
3096 		}
3097 
3098 		ceph_osdmap_destroy(osdc->osdmap);
3099 		osdc->osdmap = newmap;
3100 	}
3101 
3102 	was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3103 	scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3104 		      need_resend, need_resend_linger);
3105 
3106 	for (n = rb_first(&osdc->osds); n; ) {
3107 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3108 
3109 		n = rb_next(n); /* close_osd() */
3110 
3111 		scan_requests(osd, skipped_map, was_full, true, need_resend,
3112 			      need_resend_linger);
3113 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3114 		    memcmp(&osd->o_con.peer_addr,
3115 			   ceph_osd_addr(osdc->osdmap, osd->o_osd),
3116 			   sizeof(struct ceph_entity_addr)))
3117 			close_osd(osd);
3118 	}
3119 
3120 	return 0;
3121 }
3122 
3123 static void kick_requests(struct ceph_osd_client *osdc,
3124 			  struct rb_root *need_resend,
3125 			  struct list_head *need_resend_linger)
3126 {
3127 	struct ceph_osd_linger_request *lreq, *nlreq;
3128 	struct rb_node *n;
3129 
3130 	for (n = rb_first(need_resend); n; ) {
3131 		struct ceph_osd_request *req =
3132 		    rb_entry(n, struct ceph_osd_request, r_node);
3133 		struct ceph_osd *osd;
3134 
3135 		n = rb_next(n);
3136 		erase_request(need_resend, req); /* before link_request() */
3137 
3138 		WARN_ON(req->r_osd);
3139 		calc_target(osdc, &req->r_t, NULL, false);
3140 		osd = lookup_create_osd(osdc, req->r_t.osd, true);
3141 		link_request(osd, req);
3142 		if (!req->r_linger) {
3143 			if (!osd_homeless(osd) && !req->r_t.paused)
3144 				send_request(req);
3145 		} else {
3146 			cancel_linger_request(req);
3147 		}
3148 	}
3149 
3150 	list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3151 		if (!osd_homeless(lreq->osd))
3152 			send_linger(lreq);
3153 
3154 		list_del_init(&lreq->scan_item);
3155 	}
3156 }
3157 
3158 /*
3159  * Process updated osd map.
3160  *
3161  * The message contains any number of incremental and full maps, normally
3162  * indicating some sort of topology change in the cluster.  Kick requests
3163  * off to different OSDs as needed.
3164  */
3165 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3166 {
3167 	void *p = msg->front.iov_base;
3168 	void *const end = p + msg->front.iov_len;
3169 	u32 nr_maps, maplen;
3170 	u32 epoch;
3171 	struct ceph_fsid fsid;
3172 	struct rb_root need_resend = RB_ROOT;
3173 	LIST_HEAD(need_resend_linger);
3174 	bool handled_incremental = false;
3175 	bool was_pauserd, was_pausewr;
3176 	bool pauserd, pausewr;
3177 	int err;
3178 
3179 	dout("%s have %u\n", __func__, osdc->osdmap->epoch);
3180 	down_write(&osdc->lock);
3181 
3182 	/* verify fsid */
3183 	ceph_decode_need(&p, end, sizeof(fsid), bad);
3184 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
3185 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
3186 		goto bad;
3187 
3188 	was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3189 	was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3190 		      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3191 		      have_pool_full(osdc);
3192 
3193 	/* incremental maps */
3194 	ceph_decode_32_safe(&p, end, nr_maps, bad);
3195 	dout(" %d inc maps\n", nr_maps);
3196 	while (nr_maps > 0) {
3197 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3198 		epoch = ceph_decode_32(&p);
3199 		maplen = ceph_decode_32(&p);
3200 		ceph_decode_need(&p, end, maplen, bad);
3201 		if (osdc->osdmap->epoch &&
3202 		    osdc->osdmap->epoch + 1 == epoch) {
3203 			dout("applying incremental map %u len %d\n",
3204 			     epoch, maplen);
3205 			err = handle_one_map(osdc, p, p + maplen, true,
3206 					     &need_resend, &need_resend_linger);
3207 			if (err)
3208 				goto bad;
3209 			handled_incremental = true;
3210 		} else {
3211 			dout("ignoring incremental map %u len %d\n",
3212 			     epoch, maplen);
3213 		}
3214 		p += maplen;
3215 		nr_maps--;
3216 	}
3217 	if (handled_incremental)
3218 		goto done;
3219 
3220 	/* full maps */
3221 	ceph_decode_32_safe(&p, end, nr_maps, bad);
3222 	dout(" %d full maps\n", nr_maps);
3223 	while (nr_maps) {
3224 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3225 		epoch = ceph_decode_32(&p);
3226 		maplen = ceph_decode_32(&p);
3227 		ceph_decode_need(&p, end, maplen, bad);
3228 		if (nr_maps > 1) {
3229 			dout("skipping non-latest full map %u len %d\n",
3230 			     epoch, maplen);
3231 		} else if (osdc->osdmap->epoch >= epoch) {
3232 			dout("skipping full map %u len %d, "
3233 			     "older than our %u\n", epoch, maplen,
3234 			     osdc->osdmap->epoch);
3235 		} else {
3236 			dout("taking full map %u len %d\n", epoch, maplen);
3237 			err = handle_one_map(osdc, p, p + maplen, false,
3238 					     &need_resend, &need_resend_linger);
3239 			if (err)
3240 				goto bad;
3241 		}
3242 		p += maplen;
3243 		nr_maps--;
3244 	}
3245 
3246 done:
3247 	/*
3248 	 * subscribe to subsequent osdmap updates if full to ensure
3249 	 * we find out when we are no longer full and stop returning
3250 	 * ENOSPC.
3251 	 */
3252 	pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3253 	pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3254 		  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3255 		  have_pool_full(osdc);
3256 	if (was_pauserd || was_pausewr || pauserd || pausewr)
3257 		maybe_request_map(osdc);
3258 
3259 	kick_requests(osdc, &need_resend, &need_resend_linger);
3260 
3261 	ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3262 			  osdc->osdmap->epoch);
3263 	up_write(&osdc->lock);
3264 	wake_up_all(&osdc->client->auth_wq);
3265 	return;
3266 
3267 bad:
3268 	pr_err("osdc handle_map corrupt msg\n");
3269 	ceph_msg_dump(msg);
3270 	up_write(&osdc->lock);
3271 }
3272 
3273 /*
3274  * Resubmit requests pending on the given osd.
3275  */
3276 static void kick_osd_requests(struct ceph_osd *osd)
3277 {
3278 	struct rb_node *n;
3279 
3280 	for (n = rb_first(&osd->o_requests); n; ) {
3281 		struct ceph_osd_request *req =
3282 		    rb_entry(n, struct ceph_osd_request, r_node);
3283 
3284 		n = rb_next(n); /* cancel_linger_request() */
3285 
3286 		if (!req->r_linger) {
3287 			if (!req->r_t.paused)
3288 				send_request(req);
3289 		} else {
3290 			cancel_linger_request(req);
3291 		}
3292 	}
3293 	for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3294 		struct ceph_osd_linger_request *lreq =
3295 		    rb_entry(n, struct ceph_osd_linger_request, node);
3296 
3297 		send_linger(lreq);
3298 	}
3299 }
3300 
3301 /*
3302  * If the osd connection drops, we need to resubmit all requests.
3303  */
3304 static void osd_fault(struct ceph_connection *con)
3305 {
3306 	struct ceph_osd *osd = con->private;
3307 	struct ceph_osd_client *osdc = osd->o_osdc;
3308 
3309 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
3310 
3311 	down_write(&osdc->lock);
3312 	if (!osd_registered(osd)) {
3313 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
3314 		goto out_unlock;
3315 	}
3316 
3317 	if (!reopen_osd(osd))
3318 		kick_osd_requests(osd);
3319 	maybe_request_map(osdc);
3320 
3321 out_unlock:
3322 	up_write(&osdc->lock);
3323 }
3324 
3325 /*
3326  * Process osd watch notifications
3327  */
3328 static void handle_watch_notify(struct ceph_osd_client *osdc,
3329 				struct ceph_msg *msg)
3330 {
3331 	void *p = msg->front.iov_base;
3332 	void *const end = p + msg->front.iov_len;
3333 	struct ceph_osd_linger_request *lreq;
3334 	struct linger_work *lwork;
3335 	u8 proto_ver, opcode;
3336 	u64 cookie, notify_id;
3337 	u64 notifier_id = 0;
3338 	s32 return_code = 0;
3339 	void *payload = NULL;
3340 	u32 payload_len = 0;
3341 
3342 	ceph_decode_8_safe(&p, end, proto_ver, bad);
3343 	ceph_decode_8_safe(&p, end, opcode, bad);
3344 	ceph_decode_64_safe(&p, end, cookie, bad);
3345 	p += 8; /* skip ver */
3346 	ceph_decode_64_safe(&p, end, notify_id, bad);
3347 
3348 	if (proto_ver >= 1) {
3349 		ceph_decode_32_safe(&p, end, payload_len, bad);
3350 		ceph_decode_need(&p, end, payload_len, bad);
3351 		payload = p;
3352 		p += payload_len;
3353 	}
3354 
3355 	if (le16_to_cpu(msg->hdr.version) >= 2)
3356 		ceph_decode_32_safe(&p, end, return_code, bad);
3357 
3358 	if (le16_to_cpu(msg->hdr.version) >= 3)
3359 		ceph_decode_64_safe(&p, end, notifier_id, bad);
3360 
3361 	down_read(&osdc->lock);
3362 	lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
3363 	if (!lreq) {
3364 		dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
3365 		     cookie);
3366 		goto out_unlock_osdc;
3367 	}
3368 
3369 	mutex_lock(&lreq->lock);
3370 	dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
3371 	     opcode, cookie, lreq, lreq->is_watch);
3372 	if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
3373 		if (!lreq->last_error) {
3374 			lreq->last_error = -ENOTCONN;
3375 			queue_watch_error(lreq);
3376 		}
3377 	} else if (!lreq->is_watch) {
3378 		/* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
3379 		if (lreq->notify_id && lreq->notify_id != notify_id) {
3380 			dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
3381 			     lreq->notify_id, notify_id);
3382 		} else if (!completion_done(&lreq->notify_finish_wait)) {
3383 			struct ceph_msg_data *data =
3384 			    list_first_entry_or_null(&msg->data,
3385 						     struct ceph_msg_data,
3386 						     links);
3387 
3388 			if (data) {
3389 				if (lreq->preply_pages) {
3390 					WARN_ON(data->type !=
3391 							CEPH_MSG_DATA_PAGES);
3392 					*lreq->preply_pages = data->pages;
3393 					*lreq->preply_len = data->length;
3394 				} else {
3395 					ceph_release_page_vector(data->pages,
3396 					       calc_pages_for(0, data->length));
3397 				}
3398 			}
3399 			lreq->notify_finish_error = return_code;
3400 			complete_all(&lreq->notify_finish_wait);
3401 		}
3402 	} else {
3403 		/* CEPH_WATCH_EVENT_NOTIFY */
3404 		lwork = lwork_alloc(lreq, do_watch_notify);
3405 		if (!lwork) {
3406 			pr_err("failed to allocate notify-lwork\n");
3407 			goto out_unlock_lreq;
3408 		}
3409 
3410 		lwork->notify.notify_id = notify_id;
3411 		lwork->notify.notifier_id = notifier_id;
3412 		lwork->notify.payload = payload;
3413 		lwork->notify.payload_len = payload_len;
3414 		lwork->notify.msg = ceph_msg_get(msg);
3415 		lwork_queue(lwork);
3416 	}
3417 
3418 out_unlock_lreq:
3419 	mutex_unlock(&lreq->lock);
3420 out_unlock_osdc:
3421 	up_read(&osdc->lock);
3422 	return;
3423 
3424 bad:
3425 	pr_err("osdc handle_watch_notify corrupt msg\n");
3426 }
3427 
3428 /*
3429  * Register request, send initial attempt.
3430  */
3431 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
3432 			    struct ceph_osd_request *req,
3433 			    bool nofail)
3434 {
3435 	down_read(&osdc->lock);
3436 	submit_request(req, false);
3437 	up_read(&osdc->lock);
3438 
3439 	return 0;
3440 }
3441 EXPORT_SYMBOL(ceph_osdc_start_request);
3442 
3443 /*
3444  * Unregister a registered request.  The request is not completed:
3445  * ->r_result isn't set and __complete_request() isn't called.
3446  */
3447 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
3448 {
3449 	struct ceph_osd_client *osdc = req->r_osdc;
3450 
3451 	down_write(&osdc->lock);
3452 	if (req->r_osd)
3453 		cancel_request(req);
3454 	up_write(&osdc->lock);
3455 }
3456 EXPORT_SYMBOL(ceph_osdc_cancel_request);
3457 
3458 /*
3459  * @timeout: in jiffies, 0 means "wait forever"
3460  */
3461 static int wait_request_timeout(struct ceph_osd_request *req,
3462 				unsigned long timeout)
3463 {
3464 	long left;
3465 
3466 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3467 	left = wait_for_completion_killable_timeout(&req->r_completion,
3468 						ceph_timeout_jiffies(timeout));
3469 	if (left <= 0) {
3470 		left = left ?: -ETIMEDOUT;
3471 		ceph_osdc_cancel_request(req);
3472 	} else {
3473 		left = req->r_result; /* completed */
3474 	}
3475 
3476 	return left;
3477 }
3478 
3479 /*
3480  * wait for a request to complete
3481  */
3482 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
3483 			   struct ceph_osd_request *req)
3484 {
3485 	return wait_request_timeout(req, 0);
3486 }
3487 EXPORT_SYMBOL(ceph_osdc_wait_request);
3488 
3489 /*
3490  * sync - wait for all in-flight requests to flush.  avoid starvation.
3491  */
3492 void ceph_osdc_sync(struct ceph_osd_client *osdc)
3493 {
3494 	struct rb_node *n, *p;
3495 	u64 last_tid = atomic64_read(&osdc->last_tid);
3496 
3497 again:
3498 	down_read(&osdc->lock);
3499 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3500 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3501 
3502 		mutex_lock(&osd->lock);
3503 		for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
3504 			struct ceph_osd_request *req =
3505 			    rb_entry(p, struct ceph_osd_request, r_node);
3506 
3507 			if (req->r_tid > last_tid)
3508 				break;
3509 
3510 			if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
3511 				continue;
3512 
3513 			ceph_osdc_get_request(req);
3514 			mutex_unlock(&osd->lock);
3515 			up_read(&osdc->lock);
3516 			dout("%s waiting on req %p tid %llu last_tid %llu\n",
3517 			     __func__, req, req->r_tid, last_tid);
3518 			wait_for_completion(&req->r_completion);
3519 			ceph_osdc_put_request(req);
3520 			goto again;
3521 		}
3522 
3523 		mutex_unlock(&osd->lock);
3524 	}
3525 
3526 	up_read(&osdc->lock);
3527 	dout("%s done last_tid %llu\n", __func__, last_tid);
3528 }
3529 EXPORT_SYMBOL(ceph_osdc_sync);
3530 
3531 static struct ceph_osd_request *
3532 alloc_linger_request(struct ceph_osd_linger_request *lreq)
3533 {
3534 	struct ceph_osd_request *req;
3535 
3536 	req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
3537 	if (!req)
3538 		return NULL;
3539 
3540 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3541 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3542 
3543 	if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
3544 		ceph_osdc_put_request(req);
3545 		return NULL;
3546 	}
3547 
3548 	return req;
3549 }
3550 
3551 /*
3552  * Returns a handle, caller owns a ref.
3553  */
3554 struct ceph_osd_linger_request *
3555 ceph_osdc_watch(struct ceph_osd_client *osdc,
3556 		struct ceph_object_id *oid,
3557 		struct ceph_object_locator *oloc,
3558 		rados_watchcb2_t wcb,
3559 		rados_watcherrcb_t errcb,
3560 		void *data)
3561 {
3562 	struct ceph_osd_linger_request *lreq;
3563 	int ret;
3564 
3565 	lreq = linger_alloc(osdc);
3566 	if (!lreq)
3567 		return ERR_PTR(-ENOMEM);
3568 
3569 	lreq->is_watch = true;
3570 	lreq->wcb = wcb;
3571 	lreq->errcb = errcb;
3572 	lreq->data = data;
3573 	lreq->watch_valid_thru = jiffies;
3574 
3575 	ceph_oid_copy(&lreq->t.base_oid, oid);
3576 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
3577 	lreq->t.flags = CEPH_OSD_FLAG_WRITE;
3578 	lreq->mtime = CURRENT_TIME;
3579 
3580 	lreq->reg_req = alloc_linger_request(lreq);
3581 	if (!lreq->reg_req) {
3582 		ret = -ENOMEM;
3583 		goto err_put_lreq;
3584 	}
3585 
3586 	lreq->ping_req = alloc_linger_request(lreq);
3587 	if (!lreq->ping_req) {
3588 		ret = -ENOMEM;
3589 		goto err_put_lreq;
3590 	}
3591 
3592 	down_write(&osdc->lock);
3593 	linger_register(lreq); /* before osd_req_op_* */
3594 	osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
3595 			      CEPH_OSD_WATCH_OP_WATCH);
3596 	osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
3597 			      CEPH_OSD_WATCH_OP_PING);
3598 	linger_submit(lreq);
3599 	up_write(&osdc->lock);
3600 
3601 	ret = linger_reg_commit_wait(lreq);
3602 	if (ret) {
3603 		linger_cancel(lreq);
3604 		goto err_put_lreq;
3605 	}
3606 
3607 	return lreq;
3608 
3609 err_put_lreq:
3610 	linger_put(lreq);
3611 	return ERR_PTR(ret);
3612 }
3613 EXPORT_SYMBOL(ceph_osdc_watch);
3614 
3615 /*
3616  * Releases a ref.
3617  *
3618  * Times out after mount_timeout to preserve rbd unmap behaviour
3619  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
3620  * with mount_timeout").
3621  */
3622 int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
3623 		      struct ceph_osd_linger_request *lreq)
3624 {
3625 	struct ceph_options *opts = osdc->client->options;
3626 	struct ceph_osd_request *req;
3627 	int ret;
3628 
3629 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3630 	if (!req)
3631 		return -ENOMEM;
3632 
3633 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
3634 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
3635 	req->r_flags = CEPH_OSD_FLAG_WRITE;
3636 	req->r_mtime = CURRENT_TIME;
3637 	osd_req_op_watch_init(req, 0, lreq->linger_id,
3638 			      CEPH_OSD_WATCH_OP_UNWATCH);
3639 
3640 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3641 	if (ret)
3642 		goto out_put_req;
3643 
3644 	ceph_osdc_start_request(osdc, req, false);
3645 	linger_cancel(lreq);
3646 	linger_put(lreq);
3647 	ret = wait_request_timeout(req, opts->mount_timeout);
3648 
3649 out_put_req:
3650 	ceph_osdc_put_request(req);
3651 	return ret;
3652 }
3653 EXPORT_SYMBOL(ceph_osdc_unwatch);
3654 
3655 static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
3656 				      u64 notify_id, u64 cookie, void *payload,
3657 				      size_t payload_len)
3658 {
3659 	struct ceph_osd_req_op *op;
3660 	struct ceph_pagelist *pl;
3661 	int ret;
3662 
3663 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
3664 
3665 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
3666 	if (!pl)
3667 		return -ENOMEM;
3668 
3669 	ceph_pagelist_init(pl);
3670 	ret = ceph_pagelist_encode_64(pl, notify_id);
3671 	ret |= ceph_pagelist_encode_64(pl, cookie);
3672 	if (payload) {
3673 		ret |= ceph_pagelist_encode_32(pl, payload_len);
3674 		ret |= ceph_pagelist_append(pl, payload, payload_len);
3675 	} else {
3676 		ret |= ceph_pagelist_encode_32(pl, 0);
3677 	}
3678 	if (ret) {
3679 		ceph_pagelist_release(pl);
3680 		return -ENOMEM;
3681 	}
3682 
3683 	ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
3684 	op->indata_len = pl->length;
3685 	return 0;
3686 }
3687 
3688 int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
3689 			 struct ceph_object_id *oid,
3690 			 struct ceph_object_locator *oloc,
3691 			 u64 notify_id,
3692 			 u64 cookie,
3693 			 void *payload,
3694 			 size_t payload_len)
3695 {
3696 	struct ceph_osd_request *req;
3697 	int ret;
3698 
3699 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3700 	if (!req)
3701 		return -ENOMEM;
3702 
3703 	ceph_oid_copy(&req->r_base_oid, oid);
3704 	ceph_oloc_copy(&req->r_base_oloc, oloc);
3705 	req->r_flags = CEPH_OSD_FLAG_READ;
3706 
3707 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3708 	if (ret)
3709 		goto out_put_req;
3710 
3711 	ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
3712 					 payload_len);
3713 	if (ret)
3714 		goto out_put_req;
3715 
3716 	ceph_osdc_start_request(osdc, req, false);
3717 	ret = ceph_osdc_wait_request(osdc, req);
3718 
3719 out_put_req:
3720 	ceph_osdc_put_request(req);
3721 	return ret;
3722 }
3723 EXPORT_SYMBOL(ceph_osdc_notify_ack);
3724 
3725 static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
3726 				  u64 cookie, u32 prot_ver, u32 timeout,
3727 				  void *payload, size_t payload_len)
3728 {
3729 	struct ceph_osd_req_op *op;
3730 	struct ceph_pagelist *pl;
3731 	int ret;
3732 
3733 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
3734 	op->notify.cookie = cookie;
3735 
3736 	pl = kmalloc(sizeof(*pl), GFP_NOIO);
3737 	if (!pl)
3738 		return -ENOMEM;
3739 
3740 	ceph_pagelist_init(pl);
3741 	ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
3742 	ret |= ceph_pagelist_encode_32(pl, timeout);
3743 	ret |= ceph_pagelist_encode_32(pl, payload_len);
3744 	ret |= ceph_pagelist_append(pl, payload, payload_len);
3745 	if (ret) {
3746 		ceph_pagelist_release(pl);
3747 		return -ENOMEM;
3748 	}
3749 
3750 	ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
3751 	op->indata_len = pl->length;
3752 	return 0;
3753 }
3754 
3755 /*
3756  * @timeout: in seconds
3757  *
3758  * @preply_{pages,len} are initialized both on success and error.
3759  * The caller is responsible for:
3760  *
3761  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
3762  */
3763 int ceph_osdc_notify(struct ceph_osd_client *osdc,
3764 		     struct ceph_object_id *oid,
3765 		     struct ceph_object_locator *oloc,
3766 		     void *payload,
3767 		     size_t payload_len,
3768 		     u32 timeout,
3769 		     struct page ***preply_pages,
3770 		     size_t *preply_len)
3771 {
3772 	struct ceph_osd_linger_request *lreq;
3773 	struct page **pages;
3774 	int ret;
3775 
3776 	WARN_ON(!timeout);
3777 	if (preply_pages) {
3778 		*preply_pages = NULL;
3779 		*preply_len = 0;
3780 	}
3781 
3782 	lreq = linger_alloc(osdc);
3783 	if (!lreq)
3784 		return -ENOMEM;
3785 
3786 	lreq->preply_pages = preply_pages;
3787 	lreq->preply_len = preply_len;
3788 
3789 	ceph_oid_copy(&lreq->t.base_oid, oid);
3790 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
3791 	lreq->t.flags = CEPH_OSD_FLAG_READ;
3792 
3793 	lreq->reg_req = alloc_linger_request(lreq);
3794 	if (!lreq->reg_req) {
3795 		ret = -ENOMEM;
3796 		goto out_put_lreq;
3797 	}
3798 
3799 	/* for notify_id */
3800 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
3801 	if (IS_ERR(pages)) {
3802 		ret = PTR_ERR(pages);
3803 		goto out_put_lreq;
3804 	}
3805 
3806 	down_write(&osdc->lock);
3807 	linger_register(lreq); /* before osd_req_op_* */
3808 	ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
3809 				     timeout, payload, payload_len);
3810 	if (ret) {
3811 		linger_unregister(lreq);
3812 		up_write(&osdc->lock);
3813 		ceph_release_page_vector(pages, 1);
3814 		goto out_put_lreq;
3815 	}
3816 	ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
3817 						 response_data),
3818 				 pages, PAGE_SIZE, 0, false, true);
3819 	linger_submit(lreq);
3820 	up_write(&osdc->lock);
3821 
3822 	ret = linger_reg_commit_wait(lreq);
3823 	if (!ret)
3824 		ret = linger_notify_finish_wait(lreq);
3825 	else
3826 		dout("lreq %p failed to initiate notify %d\n", lreq, ret);
3827 
3828 	linger_cancel(lreq);
3829 out_put_lreq:
3830 	linger_put(lreq);
3831 	return ret;
3832 }
3833 EXPORT_SYMBOL(ceph_osdc_notify);
3834 
3835 /*
3836  * Return the number of milliseconds since the watch was last
3837  * confirmed, or an error.  If there is an error, the watch is no
3838  * longer valid, and should be destroyed with ceph_osdc_unwatch().
3839  */
3840 int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
3841 			  struct ceph_osd_linger_request *lreq)
3842 {
3843 	unsigned long stamp, age;
3844 	int ret;
3845 
3846 	down_read(&osdc->lock);
3847 	mutex_lock(&lreq->lock);
3848 	stamp = lreq->watch_valid_thru;
3849 	if (!list_empty(&lreq->pending_lworks)) {
3850 		struct linger_work *lwork =
3851 		    list_first_entry(&lreq->pending_lworks,
3852 				     struct linger_work,
3853 				     pending_item);
3854 
3855 		if (time_before(lwork->queued_stamp, stamp))
3856 			stamp = lwork->queued_stamp;
3857 	}
3858 	age = jiffies - stamp;
3859 	dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
3860 	     lreq, lreq->linger_id, age, lreq->last_error);
3861 	/* we are truncating to msecs, so return a safe upper bound */
3862 	ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
3863 
3864 	mutex_unlock(&lreq->lock);
3865 	up_read(&osdc->lock);
3866 	return ret;
3867 }
3868 
3869 static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
3870 {
3871 	u8 struct_v;
3872 	u32 struct_len;
3873 	int ret;
3874 
3875 	ret = ceph_start_decoding(p, end, 2, "watch_item_t",
3876 				  &struct_v, &struct_len);
3877 	if (ret)
3878 		return ret;
3879 
3880 	ceph_decode_copy(p, &item->name, sizeof(item->name));
3881 	item->cookie = ceph_decode_64(p);
3882 	*p += 4; /* skip timeout_seconds */
3883 	if (struct_v >= 2) {
3884 		ceph_decode_copy(p, &item->addr, sizeof(item->addr));
3885 		ceph_decode_addr(&item->addr);
3886 	}
3887 
3888 	dout("%s %s%llu cookie %llu addr %s\n", __func__,
3889 	     ENTITY_NAME(item->name), item->cookie,
3890 	     ceph_pr_addr(&item->addr.in_addr));
3891 	return 0;
3892 }
3893 
3894 static int decode_watchers(void **p, void *end,
3895 			   struct ceph_watch_item **watchers,
3896 			   u32 *num_watchers)
3897 {
3898 	u8 struct_v;
3899 	u32 struct_len;
3900 	int i;
3901 	int ret;
3902 
3903 	ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
3904 				  &struct_v, &struct_len);
3905 	if (ret)
3906 		return ret;
3907 
3908 	*num_watchers = ceph_decode_32(p);
3909 	*watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
3910 	if (!*watchers)
3911 		return -ENOMEM;
3912 
3913 	for (i = 0; i < *num_watchers; i++) {
3914 		ret = decode_watcher(p, end, *watchers + i);
3915 		if (ret) {
3916 			kfree(*watchers);
3917 			return ret;
3918 		}
3919 	}
3920 
3921 	return 0;
3922 }
3923 
3924 /*
3925  * On success, the caller is responsible for:
3926  *
3927  *     kfree(watchers);
3928  */
3929 int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
3930 			    struct ceph_object_id *oid,
3931 			    struct ceph_object_locator *oloc,
3932 			    struct ceph_watch_item **watchers,
3933 			    u32 *num_watchers)
3934 {
3935 	struct ceph_osd_request *req;
3936 	struct page **pages;
3937 	int ret;
3938 
3939 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
3940 	if (!req)
3941 		return -ENOMEM;
3942 
3943 	ceph_oid_copy(&req->r_base_oid, oid);
3944 	ceph_oloc_copy(&req->r_base_oloc, oloc);
3945 	req->r_flags = CEPH_OSD_FLAG_READ;
3946 
3947 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
3948 	if (ret)
3949 		goto out_put_req;
3950 
3951 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
3952 	if (IS_ERR(pages)) {
3953 		ret = PTR_ERR(pages);
3954 		goto out_put_req;
3955 	}
3956 
3957 	osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
3958 	ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
3959 						 response_data),
3960 				 pages, PAGE_SIZE, 0, false, true);
3961 
3962 	ceph_osdc_start_request(osdc, req, false);
3963 	ret = ceph_osdc_wait_request(osdc, req);
3964 	if (ret >= 0) {
3965 		void *p = page_address(pages[0]);
3966 		void *const end = p + req->r_ops[0].outdata_len;
3967 
3968 		ret = decode_watchers(&p, end, watchers, num_watchers);
3969 	}
3970 
3971 out_put_req:
3972 	ceph_osdc_put_request(req);
3973 	return ret;
3974 }
3975 EXPORT_SYMBOL(ceph_osdc_list_watchers);
3976 
3977 /*
3978  * Call all pending notify callbacks - for use after a watch is
3979  * unregistered, to make sure no more callbacks for it will be invoked
3980  */
3981 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
3982 {
3983 	dout("%s osdc %p\n", __func__, osdc);
3984 	flush_workqueue(osdc->notify_wq);
3985 }
3986 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
3987 
3988 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
3989 {
3990 	down_read(&osdc->lock);
3991 	maybe_request_map(osdc);
3992 	up_read(&osdc->lock);
3993 }
3994 EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
3995 
3996 /*
3997  * Execute an OSD class method on an object.
3998  *
3999  * @flags: CEPH_OSD_FLAG_*
4000  * @resp_len: in/out param for reply length
4001  */
4002 int ceph_osdc_call(struct ceph_osd_client *osdc,
4003 		   struct ceph_object_id *oid,
4004 		   struct ceph_object_locator *oloc,
4005 		   const char *class, const char *method,
4006 		   unsigned int flags,
4007 		   struct page *req_page, size_t req_len,
4008 		   struct page *resp_page, size_t *resp_len)
4009 {
4010 	struct ceph_osd_request *req;
4011 	int ret;
4012 
4013 	if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
4014 		return -E2BIG;
4015 
4016 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4017 	if (!req)
4018 		return -ENOMEM;
4019 
4020 	ceph_oid_copy(&req->r_base_oid, oid);
4021 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4022 	req->r_flags = flags;
4023 
4024 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4025 	if (ret)
4026 		goto out_put_req;
4027 
4028 	osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4029 	if (req_page)
4030 		osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4031 						  0, false, false);
4032 	if (resp_page)
4033 		osd_req_op_cls_response_data_pages(req, 0, &resp_page,
4034 						   *resp_len, 0, false, false);
4035 
4036 	ceph_osdc_start_request(osdc, req, false);
4037 	ret = ceph_osdc_wait_request(osdc, req);
4038 	if (ret >= 0) {
4039 		ret = req->r_ops[0].rval;
4040 		if (resp_page)
4041 			*resp_len = req->r_ops[0].outdata_len;
4042 	}
4043 
4044 out_put_req:
4045 	ceph_osdc_put_request(req);
4046 	return ret;
4047 }
4048 EXPORT_SYMBOL(ceph_osdc_call);
4049 
4050 /*
4051  * init, shutdown
4052  */
4053 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
4054 {
4055 	int err;
4056 
4057 	dout("init\n");
4058 	osdc->client = client;
4059 	init_rwsem(&osdc->lock);
4060 	osdc->osds = RB_ROOT;
4061 	INIT_LIST_HEAD(&osdc->osd_lru);
4062 	spin_lock_init(&osdc->osd_lru_lock);
4063 	osd_init(&osdc->homeless_osd);
4064 	osdc->homeless_osd.o_osdc = osdc;
4065 	osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
4066 	osdc->last_linger_id = CEPH_LINGER_ID_START;
4067 	osdc->linger_requests = RB_ROOT;
4068 	osdc->map_checks = RB_ROOT;
4069 	osdc->linger_map_checks = RB_ROOT;
4070 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
4071 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
4072 
4073 	err = -ENOMEM;
4074 	osdc->osdmap = ceph_osdmap_alloc();
4075 	if (!osdc->osdmap)
4076 		goto out;
4077 
4078 	osdc->req_mempool = mempool_create_slab_pool(10,
4079 						     ceph_osd_request_cache);
4080 	if (!osdc->req_mempool)
4081 		goto out_map;
4082 
4083 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
4084 				PAGE_SIZE, 10, true, "osd_op");
4085 	if (err < 0)
4086 		goto out_mempool;
4087 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
4088 				PAGE_SIZE, 10, true, "osd_op_reply");
4089 	if (err < 0)
4090 		goto out_msgpool;
4091 
4092 	err = -ENOMEM;
4093 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
4094 	if (!osdc->notify_wq)
4095 		goto out_msgpool_reply;
4096 
4097 	schedule_delayed_work(&osdc->timeout_work,
4098 			      osdc->client->options->osd_keepalive_timeout);
4099 	schedule_delayed_work(&osdc->osds_timeout_work,
4100 	    round_jiffies_relative(osdc->client->options->osd_idle_ttl));
4101 
4102 	return 0;
4103 
4104 out_msgpool_reply:
4105 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
4106 out_msgpool:
4107 	ceph_msgpool_destroy(&osdc->msgpool_op);
4108 out_mempool:
4109 	mempool_destroy(osdc->req_mempool);
4110 out_map:
4111 	ceph_osdmap_destroy(osdc->osdmap);
4112 out:
4113 	return err;
4114 }
4115 
4116 void ceph_osdc_stop(struct ceph_osd_client *osdc)
4117 {
4118 	flush_workqueue(osdc->notify_wq);
4119 	destroy_workqueue(osdc->notify_wq);
4120 	cancel_delayed_work_sync(&osdc->timeout_work);
4121 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
4122 
4123 	down_write(&osdc->lock);
4124 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
4125 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
4126 						struct ceph_osd, o_node);
4127 		close_osd(osd);
4128 	}
4129 	up_write(&osdc->lock);
4130 	WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
4131 	osd_cleanup(&osdc->homeless_osd);
4132 
4133 	WARN_ON(!list_empty(&osdc->osd_lru));
4134 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
4135 	WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
4136 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
4137 	WARN_ON(atomic_read(&osdc->num_requests));
4138 	WARN_ON(atomic_read(&osdc->num_homeless));
4139 
4140 	ceph_osdmap_destroy(osdc->osdmap);
4141 	mempool_destroy(osdc->req_mempool);
4142 	ceph_msgpool_destroy(&osdc->msgpool_op);
4143 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
4144 }
4145 
4146 /*
4147  * Read some contiguous pages.  If we cross a stripe boundary, shorten
4148  * *plen.  Return number of bytes read, or error.
4149  */
4150 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
4151 			struct ceph_vino vino, struct ceph_file_layout *layout,
4152 			u64 off, u64 *plen,
4153 			u32 truncate_seq, u64 truncate_size,
4154 			struct page **pages, int num_pages, int page_align)
4155 {
4156 	struct ceph_osd_request *req;
4157 	int rc = 0;
4158 
4159 	dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
4160 	     vino.snap, off, *plen);
4161 	req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
4162 				    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
4163 				    NULL, truncate_seq, truncate_size,
4164 				    false);
4165 	if (IS_ERR(req))
4166 		return PTR_ERR(req);
4167 
4168 	/* it may be a short read due to an object boundary */
4169 	osd_req_op_extent_osd_data_pages(req, 0,
4170 				pages, *plen, page_align, false, false);
4171 
4172 	dout("readpages  final extent is %llu~%llu (%llu bytes align %d)\n",
4173 	     off, *plen, *plen, page_align);
4174 
4175 	rc = ceph_osdc_start_request(osdc, req, false);
4176 	if (!rc)
4177 		rc = ceph_osdc_wait_request(osdc, req);
4178 
4179 	ceph_osdc_put_request(req);
4180 	dout("readpages result %d\n", rc);
4181 	return rc;
4182 }
4183 EXPORT_SYMBOL(ceph_osdc_readpages);
4184 
4185 /*
4186  * do a synchronous write on N pages
4187  */
4188 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
4189 			 struct ceph_file_layout *layout,
4190 			 struct ceph_snap_context *snapc,
4191 			 u64 off, u64 len,
4192 			 u32 truncate_seq, u64 truncate_size,
4193 			 struct timespec *mtime,
4194 			 struct page **pages, int num_pages)
4195 {
4196 	struct ceph_osd_request *req;
4197 	int rc = 0;
4198 	int page_align = off & ~PAGE_MASK;
4199 
4200 	req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
4201 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
4202 				    snapc, truncate_seq, truncate_size,
4203 				    true);
4204 	if (IS_ERR(req))
4205 		return PTR_ERR(req);
4206 
4207 	/* it may be a short write due to an object boundary */
4208 	osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
4209 				false, false);
4210 	dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
4211 
4212 	req->r_mtime = *mtime;
4213 	rc = ceph_osdc_start_request(osdc, req, true);
4214 	if (!rc)
4215 		rc = ceph_osdc_wait_request(osdc, req);
4216 
4217 	ceph_osdc_put_request(req);
4218 	if (rc == 0)
4219 		rc = len;
4220 	dout("writepages result %d\n", rc);
4221 	return rc;
4222 }
4223 EXPORT_SYMBOL(ceph_osdc_writepages);
4224 
4225 int ceph_osdc_setup(void)
4226 {
4227 	size_t size = sizeof(struct ceph_osd_request) +
4228 	    CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
4229 
4230 	BUG_ON(ceph_osd_request_cache);
4231 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
4232 						   0, 0, NULL);
4233 
4234 	return ceph_osd_request_cache ? 0 : -ENOMEM;
4235 }
4236 EXPORT_SYMBOL(ceph_osdc_setup);
4237 
4238 void ceph_osdc_cleanup(void)
4239 {
4240 	BUG_ON(!ceph_osd_request_cache);
4241 	kmem_cache_destroy(ceph_osd_request_cache);
4242 	ceph_osd_request_cache = NULL;
4243 }
4244 EXPORT_SYMBOL(ceph_osdc_cleanup);
4245 
4246 /*
4247  * handle incoming message
4248  */
4249 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
4250 {
4251 	struct ceph_osd *osd = con->private;
4252 	struct ceph_osd_client *osdc = osd->o_osdc;
4253 	int type = le16_to_cpu(msg->hdr.type);
4254 
4255 	switch (type) {
4256 	case CEPH_MSG_OSD_MAP:
4257 		ceph_osdc_handle_map(osdc, msg);
4258 		break;
4259 	case CEPH_MSG_OSD_OPREPLY:
4260 		handle_reply(osd, msg);
4261 		break;
4262 	case CEPH_MSG_WATCH_NOTIFY:
4263 		handle_watch_notify(osdc, msg);
4264 		break;
4265 
4266 	default:
4267 		pr_err("received unknown message type %d %s\n", type,
4268 		       ceph_msg_type_name(type));
4269 	}
4270 
4271 	ceph_msg_put(msg);
4272 }
4273 
4274 /*
4275  * Lookup and return message for incoming reply.  Don't try to do
4276  * anything about a larger than preallocated data portion of the
4277  * message at the moment - for now, just skip the message.
4278  */
4279 static struct ceph_msg *get_reply(struct ceph_connection *con,
4280 				  struct ceph_msg_header *hdr,
4281 				  int *skip)
4282 {
4283 	struct ceph_osd *osd = con->private;
4284 	struct ceph_osd_client *osdc = osd->o_osdc;
4285 	struct ceph_msg *m = NULL;
4286 	struct ceph_osd_request *req;
4287 	int front_len = le32_to_cpu(hdr->front_len);
4288 	int data_len = le32_to_cpu(hdr->data_len);
4289 	u64 tid = le64_to_cpu(hdr->tid);
4290 
4291 	down_read(&osdc->lock);
4292 	if (!osd_registered(osd)) {
4293 		dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
4294 		*skip = 1;
4295 		goto out_unlock_osdc;
4296 	}
4297 	WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
4298 
4299 	mutex_lock(&osd->lock);
4300 	req = lookup_request(&osd->o_requests, tid);
4301 	if (!req) {
4302 		dout("%s osd%d tid %llu unknown, skipping\n", __func__,
4303 		     osd->o_osd, tid);
4304 		*skip = 1;
4305 		goto out_unlock_session;
4306 	}
4307 
4308 	ceph_msg_revoke_incoming(req->r_reply);
4309 
4310 	if (front_len > req->r_reply->front_alloc_len) {
4311 		pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
4312 			__func__, osd->o_osd, req->r_tid, front_len,
4313 			req->r_reply->front_alloc_len);
4314 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
4315 				 false);
4316 		if (!m)
4317 			goto out_unlock_session;
4318 		ceph_msg_put(req->r_reply);
4319 		req->r_reply = m;
4320 	}
4321 
4322 	if (data_len > req->r_reply->data_length) {
4323 		pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
4324 			__func__, osd->o_osd, req->r_tid, data_len,
4325 			req->r_reply->data_length);
4326 		m = NULL;
4327 		*skip = 1;
4328 		goto out_unlock_session;
4329 	}
4330 
4331 	m = ceph_msg_get(req->r_reply);
4332 	dout("get_reply tid %lld %p\n", tid, m);
4333 
4334 out_unlock_session:
4335 	mutex_unlock(&osd->lock);
4336 out_unlock_osdc:
4337 	up_read(&osdc->lock);
4338 	return m;
4339 }
4340 
4341 /*
4342  * TODO: switch to a msg-owned pagelist
4343  */
4344 static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
4345 {
4346 	struct ceph_msg *m;
4347 	int type = le16_to_cpu(hdr->type);
4348 	u32 front_len = le32_to_cpu(hdr->front_len);
4349 	u32 data_len = le32_to_cpu(hdr->data_len);
4350 
4351 	m = ceph_msg_new(type, front_len, GFP_NOIO, false);
4352 	if (!m)
4353 		return NULL;
4354 
4355 	if (data_len) {
4356 		struct page **pages;
4357 		struct ceph_osd_data osd_data;
4358 
4359 		pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
4360 					       GFP_NOIO);
4361 		if (IS_ERR(pages)) {
4362 			ceph_msg_put(m);
4363 			return NULL;
4364 		}
4365 
4366 		ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
4367 					 false);
4368 		ceph_osdc_msg_data_add(m, &osd_data);
4369 	}
4370 
4371 	return m;
4372 }
4373 
4374 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
4375 				  struct ceph_msg_header *hdr,
4376 				  int *skip)
4377 {
4378 	struct ceph_osd *osd = con->private;
4379 	int type = le16_to_cpu(hdr->type);
4380 
4381 	*skip = 0;
4382 	switch (type) {
4383 	case CEPH_MSG_OSD_MAP:
4384 	case CEPH_MSG_WATCH_NOTIFY:
4385 		return alloc_msg_with_page_vector(hdr);
4386 	case CEPH_MSG_OSD_OPREPLY:
4387 		return get_reply(con, hdr, skip);
4388 	default:
4389 		pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
4390 			osd->o_osd, type);
4391 		*skip = 1;
4392 		return NULL;
4393 	}
4394 }
4395 
4396 /*
4397  * Wrappers to refcount containing ceph_osd struct
4398  */
4399 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
4400 {
4401 	struct ceph_osd *osd = con->private;
4402 	if (get_osd(osd))
4403 		return con;
4404 	return NULL;
4405 }
4406 
4407 static void put_osd_con(struct ceph_connection *con)
4408 {
4409 	struct ceph_osd *osd = con->private;
4410 	put_osd(osd);
4411 }
4412 
4413 /*
4414  * authentication
4415  */
4416 /*
4417  * Note: returned pointer is the address of a structure that's
4418  * managed separately.  Caller must *not* attempt to free it.
4419  */
4420 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
4421 					int *proto, int force_new)
4422 {
4423 	struct ceph_osd *o = con->private;
4424 	struct ceph_osd_client *osdc = o->o_osdc;
4425 	struct ceph_auth_client *ac = osdc->client->monc.auth;
4426 	struct ceph_auth_handshake *auth = &o->o_auth;
4427 
4428 	if (force_new && auth->authorizer) {
4429 		ceph_auth_destroy_authorizer(auth->authorizer);
4430 		auth->authorizer = NULL;
4431 	}
4432 	if (!auth->authorizer) {
4433 		int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
4434 						      auth);
4435 		if (ret)
4436 			return ERR_PTR(ret);
4437 	} else {
4438 		int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
4439 						     auth);
4440 		if (ret)
4441 			return ERR_PTR(ret);
4442 	}
4443 	*proto = ac->protocol;
4444 
4445 	return auth;
4446 }
4447 
4448 
4449 static int verify_authorizer_reply(struct ceph_connection *con)
4450 {
4451 	struct ceph_osd *o = con->private;
4452 	struct ceph_osd_client *osdc = o->o_osdc;
4453 	struct ceph_auth_client *ac = osdc->client->monc.auth;
4454 
4455 	return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
4456 }
4457 
4458 static int invalidate_authorizer(struct ceph_connection *con)
4459 {
4460 	struct ceph_osd *o = con->private;
4461 	struct ceph_osd_client *osdc = o->o_osdc;
4462 	struct ceph_auth_client *ac = osdc->client->monc.auth;
4463 
4464 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
4465 	return ceph_monc_validate_auth(&osdc->client->monc);
4466 }
4467 
4468 static int osd_sign_message(struct ceph_msg *msg)
4469 {
4470 	struct ceph_osd *o = msg->con->private;
4471 	struct ceph_auth_handshake *auth = &o->o_auth;
4472 
4473 	return ceph_auth_sign_message(auth, msg);
4474 }
4475 
4476 static int osd_check_message_signature(struct ceph_msg *msg)
4477 {
4478 	struct ceph_osd *o = msg->con->private;
4479 	struct ceph_auth_handshake *auth = &o->o_auth;
4480 
4481 	return ceph_auth_check_message_signature(auth, msg);
4482 }
4483 
4484 static const struct ceph_connection_operations osd_con_ops = {
4485 	.get = get_osd_con,
4486 	.put = put_osd_con,
4487 	.dispatch = dispatch,
4488 	.get_authorizer = get_authorizer,
4489 	.verify_authorizer_reply = verify_authorizer_reply,
4490 	.invalidate_authorizer = invalidate_authorizer,
4491 	.alloc_msg = alloc_msg,
4492 	.sign_message = osd_sign_message,
4493 	.check_message_signature = osd_check_message_signature,
4494 	.fault = osd_fault,
4495 };
4496