PageRenderTime 165ms CodeModel.GetById 25ms app.highlight 121ms RepoModel.GetById 1ms app.codeStats 2ms

/net/ceph/osd_client.c

http://github.com/mirrors/linux
C | 5594 lines | 4352 code | 848 blank | 394 comment | 581 complexity | 677125c21d0d3db4d8aba12fbc13c94e MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1// SPDX-License-Identifier: GPL-2.0
   2
   3#include <linux/ceph/ceph_debug.h>
   4
   5#include <linux/module.h>
   6#include <linux/err.h>
   7#include <linux/highmem.h>
   8#include <linux/mm.h>
   9#include <linux/pagemap.h>
  10#include <linux/slab.h>
  11#include <linux/uaccess.h>
  12#ifdef CONFIG_BLOCK
  13#include <linux/bio.h>
  14#endif
  15
  16#include <linux/ceph/ceph_features.h>
  17#include <linux/ceph/libceph.h>
  18#include <linux/ceph/osd_client.h>
  19#include <linux/ceph/messenger.h>
  20#include <linux/ceph/decode.h>
  21#include <linux/ceph/auth.h>
  22#include <linux/ceph/pagelist.h>
  23#include <linux/ceph/striper.h>
  24
  25#define OSD_OPREPLY_FRONT_LEN	512
  26
  27static struct kmem_cache	*ceph_osd_request_cache;
  28
  29static const struct ceph_connection_operations osd_con_ops;
  30
  31/*
  32 * Implement client access to distributed object storage cluster.
  33 *
  34 * All data objects are stored within a cluster/cloud of OSDs, or
  35 * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
  36 * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
  37 * remote daemons serving up and coordinating consistent and safe
  38 * access to storage.
  39 *
  40 * Cluster membership and the mapping of data objects onto storage devices
  41 * are described by the osd map.
  42 *
  43 * We keep track of pending OSD requests (read, write), resubmit
  44 * requests to different OSDs when the cluster topology/data layout
  45 * change, or retry the affected requests when the communications
  46 * channel with an OSD is reset.
  47 */
  48
  49static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
  50static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
  51static void link_linger(struct ceph_osd *osd,
  52			struct ceph_osd_linger_request *lreq);
  53static void unlink_linger(struct ceph_osd *osd,
  54			  struct ceph_osd_linger_request *lreq);
  55static void clear_backoffs(struct ceph_osd *osd);
  56
  57#if 1
  58static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
  59{
  60	bool wrlocked = true;
  61
  62	if (unlikely(down_read_trylock(sem))) {
  63		wrlocked = false;
  64		up_read(sem);
  65	}
  66
  67	return wrlocked;
  68}
  69static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
  70{
  71	WARN_ON(!rwsem_is_locked(&osdc->lock));
  72}
  73static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
  74{
  75	WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
  76}
  77static inline void verify_osd_locked(struct ceph_osd *osd)
  78{
  79	struct ceph_osd_client *osdc = osd->o_osdc;
  80
  81	WARN_ON(!(mutex_is_locked(&osd->lock) &&
  82		  rwsem_is_locked(&osdc->lock)) &&
  83		!rwsem_is_wrlocked(&osdc->lock));
  84}
  85static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
  86{
  87	WARN_ON(!mutex_is_locked(&lreq->lock));
  88}
  89#else
  90static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
  91static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
  92static inline void verify_osd_locked(struct ceph_osd *osd) { }
  93static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
  94#endif
  95
  96/*
  97 * calculate the mapping of a file extent onto an object, and fill out the
  98 * request accordingly.  shorten extent as necessary if it crosses an
  99 * object boundary.
 100 *
 101 * fill osd op in request message.
 102 */
 103static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
 104			u64 *objnum, u64 *objoff, u64 *objlen)
 105{
 106	u64 orig_len = *plen;
 107	u32 xlen;
 108
 109	/* object extent? */
 110	ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
 111					  objoff, &xlen);
 112	*objlen = xlen;
 113	if (*objlen < orig_len) {
 114		*plen = *objlen;
 115		dout(" skipping last %llu, final file extent %llu~%llu\n",
 116		     orig_len - *plen, off, *plen);
 117	}
 118
 119	dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
 120	return 0;
 121}
 122
 123static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
 124{
 125	memset(osd_data, 0, sizeof (*osd_data));
 126	osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
 127}
 128
 129/*
 130 * Consumes @pages if @own_pages is true.
 131 */
 132static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
 133			struct page **pages, u64 length, u32 alignment,
 134			bool pages_from_pool, bool own_pages)
 135{
 136	osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
 137	osd_data->pages = pages;
 138	osd_data->length = length;
 139	osd_data->alignment = alignment;
 140	osd_data->pages_from_pool = pages_from_pool;
 141	osd_data->own_pages = own_pages;
 142}
 143
 144/*
 145 * Consumes a ref on @pagelist.
 146 */
 147static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
 148			struct ceph_pagelist *pagelist)
 149{
 150	osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
 151	osd_data->pagelist = pagelist;
 152}
 153
 154#ifdef CONFIG_BLOCK
 155static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
 156				   struct ceph_bio_iter *bio_pos,
 157				   u32 bio_length)
 158{
 159	osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
 160	osd_data->bio_pos = *bio_pos;
 161	osd_data->bio_length = bio_length;
 162}
 163#endif /* CONFIG_BLOCK */
 164
 165static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
 166				     struct ceph_bvec_iter *bvec_pos,
 167				     u32 num_bvecs)
 168{
 169	osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
 170	osd_data->bvec_pos = *bvec_pos;
 171	osd_data->num_bvecs = num_bvecs;
 172}
 173
 174static struct ceph_osd_data *
 175osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
 176{
 177	BUG_ON(which >= osd_req->r_num_ops);
 178
 179	return &osd_req->r_ops[which].raw_data_in;
 180}
 181
 182struct ceph_osd_data *
 183osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
 184			unsigned int which)
 185{
 186	return osd_req_op_data(osd_req, which, extent, osd_data);
 187}
 188EXPORT_SYMBOL(osd_req_op_extent_osd_data);
 189
 190void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
 191			unsigned int which, struct page **pages,
 192			u64 length, u32 alignment,
 193			bool pages_from_pool, bool own_pages)
 194{
 195	struct ceph_osd_data *osd_data;
 196
 197	osd_data = osd_req_op_raw_data_in(osd_req, which);
 198	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
 199				pages_from_pool, own_pages);
 200}
 201EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
 202
 203void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
 204			unsigned int which, struct page **pages,
 205			u64 length, u32 alignment,
 206			bool pages_from_pool, bool own_pages)
 207{
 208	struct ceph_osd_data *osd_data;
 209
 210	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
 211	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
 212				pages_from_pool, own_pages);
 213}
 214EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
 215
 216void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
 217			unsigned int which, struct ceph_pagelist *pagelist)
 218{
 219	struct ceph_osd_data *osd_data;
 220
 221	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
 222	ceph_osd_data_pagelist_init(osd_data, pagelist);
 223}
 224EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
 225
 226#ifdef CONFIG_BLOCK
 227void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
 228				    unsigned int which,
 229				    struct ceph_bio_iter *bio_pos,
 230				    u32 bio_length)
 231{
 232	struct ceph_osd_data *osd_data;
 233
 234	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
 235	ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
 236}
 237EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
 238#endif /* CONFIG_BLOCK */
 239
 240void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
 241				      unsigned int which,
 242				      struct bio_vec *bvecs, u32 num_bvecs,
 243				      u32 bytes)
 244{
 245	struct ceph_osd_data *osd_data;
 246	struct ceph_bvec_iter it = {
 247		.bvecs = bvecs,
 248		.iter = { .bi_size = bytes },
 249	};
 250
 251	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
 252	ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
 253}
 254EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
 255
 256void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
 257					 unsigned int which,
 258					 struct ceph_bvec_iter *bvec_pos)
 259{
 260	struct ceph_osd_data *osd_data;
 261
 262	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
 263	ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
 264}
 265EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
 266
 267static void osd_req_op_cls_request_info_pagelist(
 268			struct ceph_osd_request *osd_req,
 269			unsigned int which, struct ceph_pagelist *pagelist)
 270{
 271	struct ceph_osd_data *osd_data;
 272
 273	osd_data = osd_req_op_data(osd_req, which, cls, request_info);
 274	ceph_osd_data_pagelist_init(osd_data, pagelist);
 275}
 276
 277void osd_req_op_cls_request_data_pagelist(
 278			struct ceph_osd_request *osd_req,
 279			unsigned int which, struct ceph_pagelist *pagelist)
 280{
 281	struct ceph_osd_data *osd_data;
 282
 283	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
 284	ceph_osd_data_pagelist_init(osd_data, pagelist);
 285	osd_req->r_ops[which].cls.indata_len += pagelist->length;
 286	osd_req->r_ops[which].indata_len += pagelist->length;
 287}
 288EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
 289
 290void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
 291			unsigned int which, struct page **pages, u64 length,
 292			u32 alignment, bool pages_from_pool, bool own_pages)
 293{
 294	struct ceph_osd_data *osd_data;
 295
 296	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
 297	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
 298				pages_from_pool, own_pages);
 299	osd_req->r_ops[which].cls.indata_len += length;
 300	osd_req->r_ops[which].indata_len += length;
 301}
 302EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
 303
 304void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
 305				       unsigned int which,
 306				       struct bio_vec *bvecs, u32 num_bvecs,
 307				       u32 bytes)
 308{
 309	struct ceph_osd_data *osd_data;
 310	struct ceph_bvec_iter it = {
 311		.bvecs = bvecs,
 312		.iter = { .bi_size = bytes },
 313	};
 314
 315	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
 316	ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
 317	osd_req->r_ops[which].cls.indata_len += bytes;
 318	osd_req->r_ops[which].indata_len += bytes;
 319}
 320EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
 321
 322void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
 323			unsigned int which, struct page **pages, u64 length,
 324			u32 alignment, bool pages_from_pool, bool own_pages)
 325{
 326	struct ceph_osd_data *osd_data;
 327
 328	osd_data = osd_req_op_data(osd_req, which, cls, response_data);
 329	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
 330				pages_from_pool, own_pages);
 331}
 332EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
 333
 334static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
 335{
 336	switch (osd_data->type) {
 337	case CEPH_OSD_DATA_TYPE_NONE:
 338		return 0;
 339	case CEPH_OSD_DATA_TYPE_PAGES:
 340		return osd_data->length;
 341	case CEPH_OSD_DATA_TYPE_PAGELIST:
 342		return (u64)osd_data->pagelist->length;
 343#ifdef CONFIG_BLOCK
 344	case CEPH_OSD_DATA_TYPE_BIO:
 345		return (u64)osd_data->bio_length;
 346#endif /* CONFIG_BLOCK */
 347	case CEPH_OSD_DATA_TYPE_BVECS:
 348		return osd_data->bvec_pos.iter.bi_size;
 349	default:
 350		WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
 351		return 0;
 352	}
 353}
 354
 355static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
 356{
 357	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
 358		int num_pages;
 359
 360		num_pages = calc_pages_for((u64)osd_data->alignment,
 361						(u64)osd_data->length);
 362		ceph_release_page_vector(osd_data->pages, num_pages);
 363	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
 364		ceph_pagelist_release(osd_data->pagelist);
 365	}
 366	ceph_osd_data_init(osd_data);
 367}
 368
 369static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
 370			unsigned int which)
 371{
 372	struct ceph_osd_req_op *op;
 373
 374	BUG_ON(which >= osd_req->r_num_ops);
 375	op = &osd_req->r_ops[which];
 376
 377	switch (op->op) {
 378	case CEPH_OSD_OP_READ:
 379	case CEPH_OSD_OP_WRITE:
 380	case CEPH_OSD_OP_WRITEFULL:
 381		ceph_osd_data_release(&op->extent.osd_data);
 382		break;
 383	case CEPH_OSD_OP_CALL:
 384		ceph_osd_data_release(&op->cls.request_info);
 385		ceph_osd_data_release(&op->cls.request_data);
 386		ceph_osd_data_release(&op->cls.response_data);
 387		break;
 388	case CEPH_OSD_OP_SETXATTR:
 389	case CEPH_OSD_OP_CMPXATTR:
 390		ceph_osd_data_release(&op->xattr.osd_data);
 391		break;
 392	case CEPH_OSD_OP_STAT:
 393		ceph_osd_data_release(&op->raw_data_in);
 394		break;
 395	case CEPH_OSD_OP_NOTIFY_ACK:
 396		ceph_osd_data_release(&op->notify_ack.request_data);
 397		break;
 398	case CEPH_OSD_OP_NOTIFY:
 399		ceph_osd_data_release(&op->notify.request_data);
 400		ceph_osd_data_release(&op->notify.response_data);
 401		break;
 402	case CEPH_OSD_OP_LIST_WATCHERS:
 403		ceph_osd_data_release(&op->list_watchers.response_data);
 404		break;
 405	case CEPH_OSD_OP_COPY_FROM2:
 406		ceph_osd_data_release(&op->copy_from.osd_data);
 407		break;
 408	default:
 409		break;
 410	}
 411}
 412
 413/*
 414 * Assumes @t is zero-initialized.
 415 */
 416static void target_init(struct ceph_osd_request_target *t)
 417{
 418	ceph_oid_init(&t->base_oid);
 419	ceph_oloc_init(&t->base_oloc);
 420	ceph_oid_init(&t->target_oid);
 421	ceph_oloc_init(&t->target_oloc);
 422
 423	ceph_osds_init(&t->acting);
 424	ceph_osds_init(&t->up);
 425	t->size = -1;
 426	t->min_size = -1;
 427
 428	t->osd = CEPH_HOMELESS_OSD;
 429}
 430
 431static void target_copy(struct ceph_osd_request_target *dest,
 432			const struct ceph_osd_request_target *src)
 433{
 434	ceph_oid_copy(&dest->base_oid, &src->base_oid);
 435	ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
 436	ceph_oid_copy(&dest->target_oid, &src->target_oid);
 437	ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
 438
 439	dest->pgid = src->pgid; /* struct */
 440	dest->spgid = src->spgid; /* struct */
 441	dest->pg_num = src->pg_num;
 442	dest->pg_num_mask = src->pg_num_mask;
 443	ceph_osds_copy(&dest->acting, &src->acting);
 444	ceph_osds_copy(&dest->up, &src->up);
 445	dest->size = src->size;
 446	dest->min_size = src->min_size;
 447	dest->sort_bitwise = src->sort_bitwise;
 448
 449	dest->flags = src->flags;
 450	dest->paused = src->paused;
 451
 452	dest->epoch = src->epoch;
 453	dest->last_force_resend = src->last_force_resend;
 454
 455	dest->osd = src->osd;
 456}
 457
 458static void target_destroy(struct ceph_osd_request_target *t)
 459{
 460	ceph_oid_destroy(&t->base_oid);
 461	ceph_oloc_destroy(&t->base_oloc);
 462	ceph_oid_destroy(&t->target_oid);
 463	ceph_oloc_destroy(&t->target_oloc);
 464}
 465
 466/*
 467 * requests
 468 */
 469static void request_release_checks(struct ceph_osd_request *req)
 470{
 471	WARN_ON(!RB_EMPTY_NODE(&req->r_node));
 472	WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
 473	WARN_ON(!list_empty(&req->r_private_item));
 474	WARN_ON(req->r_osd);
 475}
 476
 477static void ceph_osdc_release_request(struct kref *kref)
 478{
 479	struct ceph_osd_request *req = container_of(kref,
 480					    struct ceph_osd_request, r_kref);
 481	unsigned int which;
 482
 483	dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
 484	     req->r_request, req->r_reply);
 485	request_release_checks(req);
 486
 487	if (req->r_request)
 488		ceph_msg_put(req->r_request);
 489	if (req->r_reply)
 490		ceph_msg_put(req->r_reply);
 491
 492	for (which = 0; which < req->r_num_ops; which++)
 493		osd_req_op_data_release(req, which);
 494
 495	target_destroy(&req->r_t);
 496	ceph_put_snap_context(req->r_snapc);
 497
 498	if (req->r_mempool)
 499		mempool_free(req, req->r_osdc->req_mempool);
 500	else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
 501		kmem_cache_free(ceph_osd_request_cache, req);
 502	else
 503		kfree(req);
 504}
 505
 506void ceph_osdc_get_request(struct ceph_osd_request *req)
 507{
 508	dout("%s %p (was %d)\n", __func__, req,
 509	     kref_read(&req->r_kref));
 510	kref_get(&req->r_kref);
 511}
 512EXPORT_SYMBOL(ceph_osdc_get_request);
 513
 514void ceph_osdc_put_request(struct ceph_osd_request *req)
 515{
 516	if (req) {
 517		dout("%s %p (was %d)\n", __func__, req,
 518		     kref_read(&req->r_kref));
 519		kref_put(&req->r_kref, ceph_osdc_release_request);
 520	}
 521}
 522EXPORT_SYMBOL(ceph_osdc_put_request);
 523
 524static void request_init(struct ceph_osd_request *req)
 525{
 526	/* req only, each op is zeroed in _osd_req_op_init() */
 527	memset(req, 0, sizeof(*req));
 528
 529	kref_init(&req->r_kref);
 530	init_completion(&req->r_completion);
 531	RB_CLEAR_NODE(&req->r_node);
 532	RB_CLEAR_NODE(&req->r_mc_node);
 533	INIT_LIST_HEAD(&req->r_private_item);
 534
 535	target_init(&req->r_t);
 536}
 537
 538/*
 539 * This is ugly, but it allows us to reuse linger registration and ping
 540 * requests, keeping the structure of the code around send_linger{_ping}()
 541 * reasonable.  Setting up a min_nr=2 mempool for each linger request
 542 * and dealing with copying ops (this blasts req only, watch op remains
 543 * intact) isn't any better.
 544 */
 545static void request_reinit(struct ceph_osd_request *req)
 546{
 547	struct ceph_osd_client *osdc = req->r_osdc;
 548	bool mempool = req->r_mempool;
 549	unsigned int num_ops = req->r_num_ops;
 550	u64 snapid = req->r_snapid;
 551	struct ceph_snap_context *snapc = req->r_snapc;
 552	bool linger = req->r_linger;
 553	struct ceph_msg *request_msg = req->r_request;
 554	struct ceph_msg *reply_msg = req->r_reply;
 555
 556	dout("%s req %p\n", __func__, req);
 557	WARN_ON(kref_read(&req->r_kref) != 1);
 558	request_release_checks(req);
 559
 560	WARN_ON(kref_read(&request_msg->kref) != 1);
 561	WARN_ON(kref_read(&reply_msg->kref) != 1);
 562	target_destroy(&req->r_t);
 563
 564	request_init(req);
 565	req->r_osdc = osdc;
 566	req->r_mempool = mempool;
 567	req->r_num_ops = num_ops;
 568	req->r_snapid = snapid;
 569	req->r_snapc = snapc;
 570	req->r_linger = linger;
 571	req->r_request = request_msg;
 572	req->r_reply = reply_msg;
 573}
 574
 575struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
 576					       struct ceph_snap_context *snapc,
 577					       unsigned int num_ops,
 578					       bool use_mempool,
 579					       gfp_t gfp_flags)
 580{
 581	struct ceph_osd_request *req;
 582
 583	if (use_mempool) {
 584		BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
 585		req = mempool_alloc(osdc->req_mempool, gfp_flags);
 586	} else if (num_ops <= CEPH_OSD_SLAB_OPS) {
 587		req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
 588	} else {
 589		BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
 590		req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
 591	}
 592	if (unlikely(!req))
 593		return NULL;
 594
 595	request_init(req);
 596	req->r_osdc = osdc;
 597	req->r_mempool = use_mempool;
 598	req->r_num_ops = num_ops;
 599	req->r_snapid = CEPH_NOSNAP;
 600	req->r_snapc = ceph_get_snap_context(snapc);
 601
 602	dout("%s req %p\n", __func__, req);
 603	return req;
 604}
 605EXPORT_SYMBOL(ceph_osdc_alloc_request);
 606
 607static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
 608{
 609	return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
 610}
 611
 612static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
 613				      int num_request_data_items,
 614				      int num_reply_data_items)
 615{
 616	struct ceph_osd_client *osdc = req->r_osdc;
 617	struct ceph_msg *msg;
 618	int msg_size;
 619
 620	WARN_ON(req->r_request || req->r_reply);
 621	WARN_ON(ceph_oid_empty(&req->r_base_oid));
 622	WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
 623
 624	/* create request message */
 625	msg_size = CEPH_ENCODING_START_BLK_LEN +
 626			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
 627	msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
 628	msg_size += CEPH_ENCODING_START_BLK_LEN +
 629			sizeof(struct ceph_osd_reqid); /* reqid */
 630	msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
 631	msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
 632	msg_size += CEPH_ENCODING_START_BLK_LEN +
 633			ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
 634	msg_size += 4 + req->r_base_oid.name_len; /* oid */
 635	msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
 636	msg_size += 8; /* snapid */
 637	msg_size += 8; /* snap_seq */
 638	msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
 639	msg_size += 4 + 8; /* retry_attempt, features */
 640
 641	if (req->r_mempool)
 642		msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
 643				       num_request_data_items);
 644	else
 645		msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
 646				    num_request_data_items, gfp, true);
 647	if (!msg)
 648		return -ENOMEM;
 649
 650	memset(msg->front.iov_base, 0, msg->front.iov_len);
 651	req->r_request = msg;
 652
 653	/* create reply message */
 654	msg_size = OSD_OPREPLY_FRONT_LEN;
 655	msg_size += req->r_base_oid.name_len;
 656	msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
 657
 658	if (req->r_mempool)
 659		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
 660				       num_reply_data_items);
 661	else
 662		msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
 663				    num_reply_data_items, gfp, true);
 664	if (!msg)
 665		return -ENOMEM;
 666
 667	req->r_reply = msg;
 668
 669	return 0;
 670}
 671
 672static bool osd_req_opcode_valid(u16 opcode)
 673{
 674	switch (opcode) {
 675#define GENERATE_CASE(op, opcode, str)	case CEPH_OSD_OP_##op: return true;
 676__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
 677#undef GENERATE_CASE
 678	default:
 679		return false;
 680	}
 681}
 682
 683static void get_num_data_items(struct ceph_osd_request *req,
 684			       int *num_request_data_items,
 685			       int *num_reply_data_items)
 686{
 687	struct ceph_osd_req_op *op;
 688
 689	*num_request_data_items = 0;
 690	*num_reply_data_items = 0;
 691
 692	for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
 693		switch (op->op) {
 694		/* request */
 695		case CEPH_OSD_OP_WRITE:
 696		case CEPH_OSD_OP_WRITEFULL:
 697		case CEPH_OSD_OP_SETXATTR:
 698		case CEPH_OSD_OP_CMPXATTR:
 699		case CEPH_OSD_OP_NOTIFY_ACK:
 700		case CEPH_OSD_OP_COPY_FROM2:
 701			*num_request_data_items += 1;
 702			break;
 703
 704		/* reply */
 705		case CEPH_OSD_OP_STAT:
 706		case CEPH_OSD_OP_READ:
 707		case CEPH_OSD_OP_LIST_WATCHERS:
 708			*num_reply_data_items += 1;
 709			break;
 710
 711		/* both */
 712		case CEPH_OSD_OP_NOTIFY:
 713			*num_request_data_items += 1;
 714			*num_reply_data_items += 1;
 715			break;
 716		case CEPH_OSD_OP_CALL:
 717			*num_request_data_items += 2;
 718			*num_reply_data_items += 1;
 719			break;
 720
 721		default:
 722			WARN_ON(!osd_req_opcode_valid(op->op));
 723			break;
 724		}
 725	}
 726}
 727
 728/*
 729 * oid, oloc and OSD op opcode(s) must be filled in before this function
 730 * is called.
 731 */
 732int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
 733{
 734	int num_request_data_items, num_reply_data_items;
 735
 736	get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
 737	return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
 738					  num_reply_data_items);
 739}
 740EXPORT_SYMBOL(ceph_osdc_alloc_messages);
 741
 742/*
 743 * This is an osd op init function for opcodes that have no data or
 744 * other information associated with them.  It also serves as a
 745 * common init routine for all the other init functions, below.
 746 */
 747static struct ceph_osd_req_op *
 748_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
 749		 u16 opcode, u32 flags)
 750{
 751	struct ceph_osd_req_op *op;
 752
 753	BUG_ON(which >= osd_req->r_num_ops);
 754	BUG_ON(!osd_req_opcode_valid(opcode));
 755
 756	op = &osd_req->r_ops[which];
 757	memset(op, 0, sizeof (*op));
 758	op->op = opcode;
 759	op->flags = flags;
 760
 761	return op;
 762}
 763
 764void osd_req_op_init(struct ceph_osd_request *osd_req,
 765		     unsigned int which, u16 opcode, u32 flags)
 766{
 767	(void)_osd_req_op_init(osd_req, which, opcode, flags);
 768}
 769EXPORT_SYMBOL(osd_req_op_init);
 770
 771void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
 772				unsigned int which, u16 opcode,
 773				u64 offset, u64 length,
 774				u64 truncate_size, u32 truncate_seq)
 775{
 776	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
 777						      opcode, 0);
 778	size_t payload_len = 0;
 779
 780	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
 781	       opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
 782	       opcode != CEPH_OSD_OP_TRUNCATE);
 783
 784	op->extent.offset = offset;
 785	op->extent.length = length;
 786	op->extent.truncate_size = truncate_size;
 787	op->extent.truncate_seq = truncate_seq;
 788	if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
 789		payload_len += length;
 790
 791	op->indata_len = payload_len;
 792}
 793EXPORT_SYMBOL(osd_req_op_extent_init);
 794
 795void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
 796				unsigned int which, u64 length)
 797{
 798	struct ceph_osd_req_op *op;
 799	u64 previous;
 800
 801	BUG_ON(which >= osd_req->r_num_ops);
 802	op = &osd_req->r_ops[which];
 803	previous = op->extent.length;
 804
 805	if (length == previous)
 806		return;		/* Nothing to do */
 807	BUG_ON(length > previous);
 808
 809	op->extent.length = length;
 810	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
 811		op->indata_len -= previous - length;
 812}
 813EXPORT_SYMBOL(osd_req_op_extent_update);
 814
 815void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
 816				unsigned int which, u64 offset_inc)
 817{
 818	struct ceph_osd_req_op *op, *prev_op;
 819
 820	BUG_ON(which + 1 >= osd_req->r_num_ops);
 821
 822	prev_op = &osd_req->r_ops[which];
 823	op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
 824	/* dup previous one */
 825	op->indata_len = prev_op->indata_len;
 826	op->outdata_len = prev_op->outdata_len;
 827	op->extent = prev_op->extent;
 828	/* adjust offset */
 829	op->extent.offset += offset_inc;
 830	op->extent.length -= offset_inc;
 831
 832	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
 833		op->indata_len -= offset_inc;
 834}
 835EXPORT_SYMBOL(osd_req_op_extent_dup_last);
 836
 837int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
 838			const char *class, const char *method)
 839{
 840	struct ceph_osd_req_op *op;
 841	struct ceph_pagelist *pagelist;
 842	size_t payload_len = 0;
 843	size_t size;
 844	int ret;
 845
 846	op = _osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
 847
 848	pagelist = ceph_pagelist_alloc(GFP_NOFS);
 849	if (!pagelist)
 850		return -ENOMEM;
 851
 852	op->cls.class_name = class;
 853	size = strlen(class);
 854	BUG_ON(size > (size_t) U8_MAX);
 855	op->cls.class_len = size;
 856	ret = ceph_pagelist_append(pagelist, class, size);
 857	if (ret)
 858		goto err_pagelist_free;
 859	payload_len += size;
 860
 861	op->cls.method_name = method;
 862	size = strlen(method);
 863	BUG_ON(size > (size_t) U8_MAX);
 864	op->cls.method_len = size;
 865	ret = ceph_pagelist_append(pagelist, method, size);
 866	if (ret)
 867		goto err_pagelist_free;
 868	payload_len += size;
 869
 870	osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
 871	op->indata_len = payload_len;
 872	return 0;
 873
 874err_pagelist_free:
 875	ceph_pagelist_release(pagelist);
 876	return ret;
 877}
 878EXPORT_SYMBOL(osd_req_op_cls_init);
 879
 880int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
 881			  u16 opcode, const char *name, const void *value,
 882			  size_t size, u8 cmp_op, u8 cmp_mode)
 883{
 884	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
 885						      opcode, 0);
 886	struct ceph_pagelist *pagelist;
 887	size_t payload_len;
 888	int ret;
 889
 890	BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
 891
 892	pagelist = ceph_pagelist_alloc(GFP_NOFS);
 893	if (!pagelist)
 894		return -ENOMEM;
 895
 896	payload_len = strlen(name);
 897	op->xattr.name_len = payload_len;
 898	ret = ceph_pagelist_append(pagelist, name, payload_len);
 899	if (ret)
 900		goto err_pagelist_free;
 901
 902	op->xattr.value_len = size;
 903	ret = ceph_pagelist_append(pagelist, value, size);
 904	if (ret)
 905		goto err_pagelist_free;
 906	payload_len += size;
 907
 908	op->xattr.cmp_op = cmp_op;
 909	op->xattr.cmp_mode = cmp_mode;
 910
 911	ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
 912	op->indata_len = payload_len;
 913	return 0;
 914
 915err_pagelist_free:
 916	ceph_pagelist_release(pagelist);
 917	return ret;
 918}
 919EXPORT_SYMBOL(osd_req_op_xattr_init);
 920
 921/*
 922 * @watch_opcode: CEPH_OSD_WATCH_OP_*
 923 */
 924static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
 925				  u64 cookie, u8 watch_opcode)
 926{
 927	struct ceph_osd_req_op *op;
 928
 929	op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
 930	op->watch.cookie = cookie;
 931	op->watch.op = watch_opcode;
 932	op->watch.gen = 0;
 933}
 934
 935void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
 936				unsigned int which,
 937				u64 expected_object_size,
 938				u64 expected_write_size)
 939{
 940	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
 941						      CEPH_OSD_OP_SETALLOCHINT,
 942						      0);
 943
 944	op->alloc_hint.expected_object_size = expected_object_size;
 945	op->alloc_hint.expected_write_size = expected_write_size;
 946
 947	/*
 948	 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
 949	 * not worth a feature bit.  Set FAILOK per-op flag to make
 950	 * sure older osds don't trip over an unsupported opcode.
 951	 */
 952	op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
 953}
 954EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
 955
 956static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
 957				struct ceph_osd_data *osd_data)
 958{
 959	u64 length = ceph_osd_data_length(osd_data);
 960
 961	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
 962		BUG_ON(length > (u64) SIZE_MAX);
 963		if (length)
 964			ceph_msg_data_add_pages(msg, osd_data->pages,
 965					length, osd_data->alignment, false);
 966	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
 967		BUG_ON(!length);
 968		ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
 969#ifdef CONFIG_BLOCK
 970	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
 971		ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
 972#endif
 973	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
 974		ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
 975	} else {
 976		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
 977	}
 978}
 979
 980static u32 osd_req_encode_op(struct ceph_osd_op *dst,
 981			     const struct ceph_osd_req_op *src)
 982{
 983	switch (src->op) {
 984	case CEPH_OSD_OP_STAT:
 985		break;
 986	case CEPH_OSD_OP_READ:
 987	case CEPH_OSD_OP_WRITE:
 988	case CEPH_OSD_OP_WRITEFULL:
 989	case CEPH_OSD_OP_ZERO:
 990	case CEPH_OSD_OP_TRUNCATE:
 991		dst->extent.offset = cpu_to_le64(src->extent.offset);
 992		dst->extent.length = cpu_to_le64(src->extent.length);
 993		dst->extent.truncate_size =
 994			cpu_to_le64(src->extent.truncate_size);
 995		dst->extent.truncate_seq =
 996			cpu_to_le32(src->extent.truncate_seq);
 997		break;
 998	case CEPH_OSD_OP_CALL:
 999		dst->cls.class_len = src->cls.class_len;
1000		dst->cls.method_len = src->cls.method_len;
1001		dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
1002		break;
1003	case CEPH_OSD_OP_WATCH:
1004		dst->watch.cookie = cpu_to_le64(src->watch.cookie);
1005		dst->watch.ver = cpu_to_le64(0);
1006		dst->watch.op = src->watch.op;
1007		dst->watch.gen = cpu_to_le32(src->watch.gen);
1008		break;
1009	case CEPH_OSD_OP_NOTIFY_ACK:
1010		break;
1011	case CEPH_OSD_OP_NOTIFY:
1012		dst->notify.cookie = cpu_to_le64(src->notify.cookie);
1013		break;
1014	case CEPH_OSD_OP_LIST_WATCHERS:
1015		break;
1016	case CEPH_OSD_OP_SETALLOCHINT:
1017		dst->alloc_hint.expected_object_size =
1018		    cpu_to_le64(src->alloc_hint.expected_object_size);
1019		dst->alloc_hint.expected_write_size =
1020		    cpu_to_le64(src->alloc_hint.expected_write_size);
1021		break;
1022	case CEPH_OSD_OP_SETXATTR:
1023	case CEPH_OSD_OP_CMPXATTR:
1024		dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
1025		dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
1026		dst->xattr.cmp_op = src->xattr.cmp_op;
1027		dst->xattr.cmp_mode = src->xattr.cmp_mode;
1028		break;
1029	case CEPH_OSD_OP_CREATE:
1030	case CEPH_OSD_OP_DELETE:
1031		break;
1032	case CEPH_OSD_OP_COPY_FROM2:
1033		dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
1034		dst->copy_from.src_version =
1035			cpu_to_le64(src->copy_from.src_version);
1036		dst->copy_from.flags = src->copy_from.flags;
1037		dst->copy_from.src_fadvise_flags =
1038			cpu_to_le32(src->copy_from.src_fadvise_flags);
1039		break;
1040	default:
1041		pr_err("unsupported osd opcode %s\n",
1042			ceph_osd_op_name(src->op));
1043		WARN_ON(1);
1044
1045		return 0;
1046	}
1047
1048	dst->op = cpu_to_le16(src->op);
1049	dst->flags = cpu_to_le32(src->flags);
1050	dst->payload_len = cpu_to_le32(src->indata_len);
1051
1052	return src->indata_len;
1053}
1054
1055/*
1056 * build new request AND message, calculate layout, and adjust file
1057 * extent as needed.
1058 *
1059 * if the file was recently truncated, we include information about its
1060 * old and new size so that the object can be updated appropriately.  (we
1061 * avoid synchronously deleting truncated objects because it's slow.)
1062 */
1063struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
1064					       struct ceph_file_layout *layout,
1065					       struct ceph_vino vino,
1066					       u64 off, u64 *plen,
1067					       unsigned int which, int num_ops,
1068					       int opcode, int flags,
1069					       struct ceph_snap_context *snapc,
1070					       u32 truncate_seq,
1071					       u64 truncate_size,
1072					       bool use_mempool)
1073{
1074	struct ceph_osd_request *req;
1075	u64 objnum = 0;
1076	u64 objoff = 0;
1077	u64 objlen = 0;
1078	int r;
1079
1080	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
1081	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1082	       opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
1083
1084	req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1085					GFP_NOFS);
1086	if (!req) {
1087		r = -ENOMEM;
1088		goto fail;
1089	}
1090
1091	/* calculate max write size */
1092	r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
1093	if (r)
1094		goto fail;
1095
1096	if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1097		osd_req_op_init(req, which, opcode, 0);
1098	} else {
1099		u32 object_size = layout->object_size;
1100		u32 object_base = off - objoff;
1101		if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1102			if (truncate_size <= object_base) {
1103				truncate_size = 0;
1104			} else {
1105				truncate_size -= object_base;
1106				if (truncate_size > object_size)
1107					truncate_size = object_size;
1108			}
1109		}
1110		osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1111				       truncate_size, truncate_seq);
1112	}
1113
1114	req->r_flags = flags;
1115	req->r_base_oloc.pool = layout->pool_id;
1116	req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1117	ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
1118
1119	req->r_snapid = vino.snap;
1120	if (flags & CEPH_OSD_FLAG_WRITE)
1121		req->r_data_offset = off;
1122
1123	if (num_ops > 1)
1124		/*
1125		 * This is a special case for ceph_writepages_start(), but it
1126		 * also covers ceph_uninline_data().  If more multi-op request
1127		 * use cases emerge, we will need a separate helper.
1128		 */
1129		r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_ops, 0);
1130	else
1131		r = ceph_osdc_alloc_messages(req, GFP_NOFS);
1132	if (r)
1133		goto fail;
1134
1135	return req;
1136
1137fail:
1138	ceph_osdc_put_request(req);
1139	return ERR_PTR(r);
1140}
1141EXPORT_SYMBOL(ceph_osdc_new_request);
1142
1143/*
1144 * We keep osd requests in an rbtree, sorted by ->r_tid.
1145 */
1146DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
1147DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
1148
1149/*
1150 * Call @fn on each OSD request as long as @fn returns 0.
1151 */
1152static void for_each_request(struct ceph_osd_client *osdc,
1153			int (*fn)(struct ceph_osd_request *req, void *arg),
1154			void *arg)
1155{
1156	struct rb_node *n, *p;
1157
1158	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1159		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1160
1161		for (p = rb_first(&osd->o_requests); p; ) {
1162			struct ceph_osd_request *req =
1163			    rb_entry(p, struct ceph_osd_request, r_node);
1164
1165			p = rb_next(p);
1166			if (fn(req, arg))
1167				return;
1168		}
1169	}
1170
1171	for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
1172		struct ceph_osd_request *req =
1173		    rb_entry(p, struct ceph_osd_request, r_node);
1174
1175		p = rb_next(p);
1176		if (fn(req, arg))
1177			return;
1178	}
1179}
1180
1181static bool osd_homeless(struct ceph_osd *osd)
1182{
1183	return osd->o_osd == CEPH_HOMELESS_OSD;
1184}
1185
1186static bool osd_registered(struct ceph_osd *osd)
1187{
1188	verify_osdc_locked(osd->o_osdc);
1189
1190	return !RB_EMPTY_NODE(&osd->o_node);
1191}
1192
1193/*
1194 * Assumes @osd is zero-initialized.
1195 */
1196static void osd_init(struct ceph_osd *osd)
1197{
1198	refcount_set(&osd->o_ref, 1);
1199	RB_CLEAR_NODE(&osd->o_node);
1200	osd->o_requests = RB_ROOT;
1201	osd->o_linger_requests = RB_ROOT;
1202	osd->o_backoff_mappings = RB_ROOT;
1203	osd->o_backoffs_by_id = RB_ROOT;
1204	INIT_LIST_HEAD(&osd->o_osd_lru);
1205	INIT_LIST_HEAD(&osd->o_keepalive_item);
1206	osd->o_incarnation = 1;
1207	mutex_init(&osd->lock);
1208}
1209
1210static void osd_cleanup(struct ceph_osd *osd)
1211{
1212	WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
1213	WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1214	WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1215	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1216	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
1217	WARN_ON(!list_empty(&osd->o_osd_lru));
1218	WARN_ON(!list_empty(&osd->o_keepalive_item));
1219
1220	if (osd->o_auth.authorizer) {
1221		WARN_ON(osd_homeless(osd));
1222		ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1223	}
1224}
1225
1226/*
1227 * Track open sessions with osds.
1228 */
1229static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1230{
1231	struct ceph_osd *osd;
1232
1233	WARN_ON(onum == CEPH_HOMELESS_OSD);
1234
1235	osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
1236	osd_init(osd);
1237	osd->o_osdc = osdc;
1238	osd->o_osd = onum;
1239
1240	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1241
1242	return osd;
1243}
1244
1245static struct ceph_osd *get_osd(struct ceph_osd *osd)
1246{
1247	if (refcount_inc_not_zero(&osd->o_ref)) {
1248		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1249		     refcount_read(&osd->o_ref));
1250		return osd;
1251	} else {
1252		dout("get_osd %p FAIL\n", osd);
1253		return NULL;
1254	}
1255}
1256
1257static void put_osd(struct ceph_osd *osd)
1258{
1259	dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1260	     refcount_read(&osd->o_ref) - 1);
1261	if (refcount_dec_and_test(&osd->o_ref)) {
1262		osd_cleanup(osd);
1263		kfree(osd);
1264	}
1265}
1266
1267DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1268
1269static void __move_osd_to_lru(struct ceph_osd *osd)
1270{
1271	struct ceph_osd_client *osdc = osd->o_osdc;
1272
1273	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1274	BUG_ON(!list_empty(&osd->o_osd_lru));
1275
1276	spin_lock(&osdc->osd_lru_lock);
1277	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1278	spin_unlock(&osdc->osd_lru_lock);
1279
1280	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1281}
1282
1283static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1284{
1285	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1286	    RB_EMPTY_ROOT(&osd->o_linger_requests))
1287		__move_osd_to_lru(osd);
1288}
1289
1290static void __remove_osd_from_lru(struct ceph_osd *osd)
1291{
1292	struct ceph_osd_client *osdc = osd->o_osdc;
1293
1294	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1295
1296	spin_lock(&osdc->osd_lru_lock);
1297	if (!list_empty(&osd->o_osd_lru))
1298		list_del_init(&osd->o_osd_lru);
1299	spin_unlock(&osdc->osd_lru_lock);
1300}
1301
1302/*
1303 * Close the connection and assign any leftover requests to the
1304 * homeless session.
1305 */
1306static void close_osd(struct ceph_osd *osd)
1307{
1308	struct ceph_osd_client *osdc = osd->o_osdc;
1309	struct rb_node *n;
1310
1311	verify_osdc_wrlocked(osdc);
1312	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1313
1314	ceph_con_close(&osd->o_con);
1315
1316	for (n = rb_first(&osd->o_requests); n; ) {
1317		struct ceph_osd_request *req =
1318		    rb_entry(n, struct ceph_osd_request, r_node);
1319
1320		n = rb_next(n); /* unlink_request() */
1321
1322		dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1323		unlink_request(osd, req);
1324		link_request(&osdc->homeless_osd, req);
1325	}
1326	for (n = rb_first(&osd->o_linger_requests); n; ) {
1327		struct ceph_osd_linger_request *lreq =
1328		    rb_entry(n, struct ceph_osd_linger_request, node);
1329
1330		n = rb_next(n); /* unlink_linger() */
1331
1332		dout(" reassigning lreq %p linger_id %llu\n", lreq,
1333		     lreq->linger_id);
1334		unlink_linger(osd, lreq);
1335		link_linger(&osdc->homeless_osd, lreq);
1336	}
1337	clear_backoffs(osd);
1338
1339	__remove_osd_from_lru(osd);
1340	erase_osd(&osdc->osds, osd);
1341	put_osd(osd);
1342}
1343
1344/*
1345 * reset osd connect
1346 */
1347static int reopen_osd(struct ceph_osd *osd)
1348{
1349	struct ceph_entity_addr *peer_addr;
1350
1351	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1352
1353	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1354	    RB_EMPTY_ROOT(&osd->o_linger_requests)) {
1355		close_osd(osd);
1356		return -ENODEV;
1357	}
1358
1359	peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1360	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1361			!ceph_con_opened(&osd->o_con)) {
1362		struct rb_node *n;
1363
1364		dout("osd addr hasn't changed and connection never opened, "
1365		     "letting msgr retry\n");
1366		/* touch each r_stamp for handle_timeout()'s benfit */
1367		for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1368			struct ceph_osd_request *req =
1369			    rb_entry(n, struct ceph_osd_request, r_node);
1370			req->r_stamp = jiffies;
1371		}
1372
1373		return -EAGAIN;
1374	}
1375
1376	ceph_con_close(&osd->o_con);
1377	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1378	osd->o_incarnation++;
1379
1380	return 0;
1381}
1382
1383static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1384					  bool wrlocked)
1385{
1386	struct ceph_osd *osd;
1387
1388	if (wrlocked)
1389		verify_osdc_wrlocked(osdc);
1390	else
1391		verify_osdc_locked(osdc);
1392
1393	if (o != CEPH_HOMELESS_OSD)
1394		osd = lookup_osd(&osdc->osds, o);
1395	else
1396		osd = &osdc->homeless_osd;
1397	if (!osd) {
1398		if (!wrlocked)
1399			return ERR_PTR(-EAGAIN);
1400
1401		osd = create_osd(osdc, o);
1402		insert_osd(&osdc->osds, osd);
1403		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1404			      &osdc->osdmap->osd_addr[osd->o_osd]);
1405	}
1406
1407	dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1408	return osd;
1409}
1410
1411/*
1412 * Create request <-> OSD session relation.
1413 *
1414 * @req has to be assigned a tid, @osd may be homeless.
1415 */
1416static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1417{
1418	verify_osd_locked(osd);
1419	WARN_ON(!req->r_tid || req->r_osd);
1420	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1421	     req, req->r_tid);
1422
1423	if (!osd_homeless(osd))
1424		__remove_osd_from_lru(osd);
1425	else
1426		atomic_inc(&osd->o_osdc->num_homeless);
1427
1428	get_osd(osd);
1429	insert_request(&osd->o_requests, req);
1430	req->r_osd = osd;
1431}
1432
1433static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1434{
1435	verify_osd_locked(osd);
1436	WARN_ON(req->r_osd != osd);
1437	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1438	     req, req->r_tid);
1439
1440	req->r_osd = NULL;
1441	erase_request(&osd->o_requests, req);
1442	put_osd(osd);
1443
1444	if (!osd_homeless(osd))
1445		maybe_move_osd_to_lru(osd);
1446	else
1447		atomic_dec(&osd->o_osdc->num_homeless);
1448}
1449
1450static bool __pool_full(struct ceph_pg_pool_info *pi)
1451{
1452	return pi->flags & CEPH_POOL_FLAG_FULL;
1453}
1454
1455static bool have_pool_full(struct ceph_osd_client *osdc)
1456{
1457	struct rb_node *n;
1458
1459	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1460		struct ceph_pg_pool_info *pi =
1461		    rb_entry(n, struct ceph_pg_pool_info, node);
1462
1463		if (__pool_full(pi))
1464			return true;
1465	}
1466
1467	return false;
1468}
1469
1470static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1471{
1472	struct ceph_pg_pool_info *pi;
1473
1474	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1475	if (!pi)
1476		return false;
1477
1478	return __pool_full(pi);
1479}
1480
1481/*
1482 * Returns whether a request should be blocked from being sent
1483 * based on the current osdmap and osd_client settings.
1484 */
1485static bool target_should_be_paused(struct ceph_osd_client *osdc,
1486				    const struct ceph_osd_request_target *t,
1487				    struct ceph_pg_pool_info *pi)
1488{
1489	bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1490	bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1491		       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1492		       __pool_full(pi);
1493
1494	WARN_ON(pi->id != t->target_oloc.pool);
1495	return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1496	       ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1497	       (osdc->osdmap->epoch < osdc->epoch_barrier);
1498}
1499
1500enum calc_target_result {
1501	CALC_TARGET_NO_ACTION = 0,
1502	CALC_TARGET_NEED_RESEND,
1503	CALC_TARGET_POOL_DNE,
1504};
1505
1506static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1507					   struct ceph_osd_request_target *t,
1508					   bool any_change)
1509{
1510	struct ceph_pg_pool_info *pi;
1511	struct ceph_pg pgid, last_pgid;
1512	struct ceph_osds up, acting;
1513	bool force_resend = false;
1514	bool unpaused = false;
1515	bool legacy_change = false;
1516	bool split = false;
1517	bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1518	bool recovery_deletes = ceph_osdmap_flag(osdc,
1519						 CEPH_OSDMAP_RECOVERY_DELETES);
1520	enum calc_target_result ct_res;
1521
1522	t->epoch = osdc->osdmap->epoch;
1523	pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1524	if (!pi) {
1525		t->osd = CEPH_HOMELESS_OSD;
1526		ct_res = CALC_TARGET_POOL_DNE;
1527		goto out;
1528	}
1529
1530	if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1531		if (t->last_force_resend < pi->last_force_request_resend) {
1532			t->last_force_resend = pi->last_force_request_resend;
1533			force_resend = true;
1534		} else if (t->last_force_resend == 0) {
1535			force_resend = true;
1536		}
1537	}
1538
1539	/* apply tiering */
1540	ceph_oid_copy(&t->target_oid, &t->base_oid);
1541	ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1542	if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1543		if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1544			t->target_oloc.pool = pi->read_tier;
1545		if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1546			t->target_oloc.pool = pi->write_tier;
1547
1548		pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1549		if (!pi) {
1550			t->osd = CEPH_HOMELESS_OSD;
1551			ct_res = CALC_TARGET_POOL_DNE;
1552			goto out;
1553		}
1554	}
1555
1556	__ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
1557	last_pgid.pool = pgid.pool;
1558	last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1559
1560	ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
1561	if (any_change &&
1562	    ceph_is_new_interval(&t->acting,
1563				 &acting,
1564				 &t->up,
1565				 &up,
1566				 t->size,
1567				 pi->size,
1568				 t->min_size,
1569				 pi->min_size,
1570				 t->pg_num,
1571				 pi->pg_num,
1572				 t->sort_bitwise,
1573				 sort_bitwise,
1574				 t->recovery_deletes,
1575				 recovery_deletes,
1576				 &last_pgid))
1577		force_resend = true;
1578
1579	if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1580		t->paused = false;
1581		unpaused = true;
1582	}
1583	legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1584			ceph_osds_changed(&t->acting, &acting, any_change);
1585	if (t->pg_num)
1586		split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
1587
1588	if (legacy_change || force_resend || split) {
1589		t->pgid = pgid; /* struct */
1590		ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
1591		ceph_osds_copy(&t->acting, &acting);
1592		ceph_osds_copy(&t->up, &up);
1593		t->size = pi->size;
1594		t->min_size = pi->min_size;
1595		t->pg_num = pi->pg_num;
1596		t->pg_num_mask = pi->pg_num_mask;
1597		t->sort_bitwise = sort_bitwise;
1598		t->recovery_deletes = recovery_deletes;
1599
1600		t->osd = acting.primary;
1601	}
1602
1603	if (unpaused || legacy_change || force_resend || split)
1604		ct_res = CALC_TARGET_NEED_RESEND;
1605	else
1606		ct_res = CALC_TARGET_NO_ACTION;
1607
1608out:
1609	dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused,
1610	     legacy_change, force_resend, split, ct_res, t->osd);
1611	return ct_res;
1612}
1613
1614static struct ceph_spg_mapping *alloc_spg_mapping(void)
1615{
1616	struct ceph_spg_mapping *spg;
1617
1618	spg = kmalloc(sizeof(*spg), GFP_NOIO);
1619	if (!spg)
1620		return NULL;
1621
1622	RB_CLEAR_NODE(&spg->node);
1623	spg->backoffs = RB_ROOT;
1624	return spg;
1625}
1626
1627static void free_spg_mapping(struct ceph_spg_mapping *spg)
1628{
1629	WARN_ON(!RB_EMPTY_NODE(&spg->node));
1630	WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1631
1632	kfree(spg);
1633}
1634
1635/*
1636 * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1637 * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1638 * defined only within a specific spgid; it does not pass anything to
1639 * children on split, or to another primary.
1640 */
1641DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1642		 RB_BYPTR, const struct ceph_spg *, node)
1643
1644static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1645{
1646	return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1647}
1648
1649static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1650				   void **pkey, size_t *pkey_len)
1651{
1652	if (hoid->key_len) {
1653		*pkey = hoid->key;
1654		*pkey_len = hoid->key_len;
1655	} else {
1656		*pkey = hoid->oid;
1657		*pkey_len = hoid->oid_len;
1658	}
1659}
1660
1661static int compare_names(const void *name1, size_t name1_len,
1662			 const void *name2, size_t name2_len)
1663{
1664	int ret;
1665
1666	ret = memcmp(name1, name2, min(name1_len, name2_len));
1667	if (!ret) {
1668		if (name1_len < name2_len)
1669			ret = -1;
1670		else if (name1_len > name2_len)
1671			ret = 1;
1672	}
1673	return ret;
1674}
1675
1676static int hoid_compare(const struct ceph_hobject_id *lhs,
1677			const struct ceph_hobject_id *rhs)
1678{
1679	void *effective_key1, *effective_key2;
1680	size_t effective_key1_len, effective_key2_len;
1681	int ret;
1682
1683	if (lhs->is_max < rhs->is_max)
1684		return -1;
1685	if (lhs->is_max > rhs->is_max)
1686		return 1;
1687
1688	if (lhs->pool < rhs->pool)
1689		return -1;
1690	if (lhs->pool > rhs->pool)
1691		return 1;
1692
1693	if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1694		return -1;
1695	if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1696		return 1;
1697
1698	ret = compare_names(lhs->nspace, lhs->nspace_len,
1699			    rhs->nspace, rhs->nspace_len);
1700	if (ret)
1701		return ret;
1702
1703	hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1704	hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1705	ret = compare_names(effective_key1, effective_key1_len,
1706			    effective_key2, effective_key2_len);
1707	if (ret)
1708		return ret;
1709
1710	ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1711	if (ret)
1712		return ret;
1713
1714	if (lhs->snapid < rhs->snapid)
1715		return -1;
1716	if (lhs->snapid > rhs->snapid)
1717		return 1;
1718
1719	return 0;
1720}
1721
1722/*
1723 * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1724 * compat stuff here.
1725 *
1726 * Assumes @hoid is zero-initialized.
1727 */
1728static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1729{
1730	u8 struct_v;
1731	u32 struct_len;
1732	int ret;
1733
1734	ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1735				  &struct_len);
1736	if (ret)
1737		return ret;
1738
1739	if (struct_v < 4) {
1740		pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1741		goto e_inval;
1742	}
1743
1744	hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1745						GFP_NOIO);
1746	if (IS_ERR(hoid->key)) {
1747		ret = PTR_ERR(hoid->key);
1748		hoid->key = NULL;
1749		return ret;
1750	}
1751
1752	hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1753						GFP_NOIO);
1754	if (IS_ERR(hoid->oid)) {
1755		ret = PTR_ERR(hoid->oid);
1756		hoid->oid = NULL;
1757		return ret;
1758	}
1759
1760	ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1761	ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1762	ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1763
1764	hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1765						   GFP_NOIO);
1766	if (IS_ERR(hoid->nspace)) {
1767		ret = PTR_ERR(hoid->nspace);
1768		hoid->nspace = NULL;
1769		return ret;
1770	}
1771
1772	ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1773
1774	ceph_hoid_build_hash_cache(hoid);
1775	return 0;
1776
1777e_inval:
1778	return -EINVAL;
1779}
1780
1781static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1782{
1783	return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1784	       4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1785}
1786
1787static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1788{
1789	ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1790	ceph_encode_string(p, end, hoid->key, hoid->key_len);
1791	ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1792	ceph_encode_64(p, hoid->snapid);
1793	ceph_encode_32(p, hoid->hash);
1794	ceph_encode_8(p, hoid->is_max);
1795	ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1796	ceph_encode_64(p, hoid->pool);
1797}
1798
1799static void free_hoid(struct ceph_hobject_id *hoid)
1800{
1801	if (hoid) {
1802		kfree(hoid->key);
1803		kfree(hoid->oid);
1804		kfree(hoid->nspace);
1805		kfree(hoid);
1806	}
1807}
1808
1809static struct ceph_osd_backoff *alloc_backoff(void)
1810{
1811	struct ceph_osd_backoff *backoff;
1812
1813	backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1814	if (!backoff)
1815		return NULL;
1816
1817	RB_CLEAR_NODE(&backoff->spg_node);
1818	RB_CLEAR_NODE(&backoff->id_node);
1819	return backoff;
1820}
1821
1822static void free_backoff(struct ceph_osd_backoff *backoff)
1823{
1824	WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1825	WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1826
1827	free_hoid(backoff->begin);
1828	free_hoid(backoff->end);
1829	kfree(backoff);
1830}
1831
1832/*
1833 * Within a specific spgid, backoffs are managed by ->begin hoid.
1834 */
1835DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1836			RB_BYVAL, spg_node);
1837
1838static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1839					    const struct ceph_hobject_id *hoid)
1840{
1841	struct rb_node *n = root->rb_node;
1842
1843	while (n) {
1844		struct ceph_osd_backoff *cur =
1845		    rb_entry(n, struct ceph_osd_backoff, spg_node);

Large files files are truncated, but you can click here to view the full file