]> git.proxmox.com Git - ceph.git/blame - ceph/src/osdc/Objecter.h
update sources to v12.1.1
[ceph.git] / ceph / src / osdc / Objecter.h
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15#ifndef CEPH_OBJECTER_H
16#define CEPH_OBJECTER_H
17
18#include <condition_variable>
19#include <list>
20#include <map>
21#include <mutex>
22#include <memory>
23#include <sstream>
24#include <type_traits>
25
26#include <boost/thread/shared_mutex.hpp>
27
28#include "include/assert.h"
29#include "include/buffer.h"
30#include "include/types.h"
31#include "include/rados/rados_types.hpp"
32
33#include "common/admin_socket.h"
34#include "common/ceph_time.h"
35#include "common/ceph_timer.h"
36#include "common/Finisher.h"
37#include "common/shunique_lock.h"
38#include "common/zipkin_trace.h"
39
40#include "messages/MOSDOp.h"
41#include "osd/OSDMap.h"
42
43using namespace std;
44
45class Context;
46class Messenger;
47class OSDMap;
48class MonClient;
49class Message;
50class Finisher;
51
52class MPoolOpReply;
53
54class MGetPoolStatsReply;
55class MStatfsReply;
56class MCommandReply;
57class MWatchNotify;
58
59class PerfCounters;
60
61// -----------------------------------------
62
63struct ObjectOperation {
64 vector<OSDOp> ops;
65 int flags;
66 int priority;
67
68 vector<bufferlist*> out_bl;
69 vector<Context*> out_handler;
70 vector<int*> out_rval;
71
72 ObjectOperation() : flags(0), priority(0) {}
73 ~ObjectOperation() {
74 while (!out_handler.empty()) {
75 delete out_handler.back();
76 out_handler.pop_back();
77 }
78 }
79
80 size_t size() {
81 return ops.size();
82 }
83
84 void set_last_op_flags(int flags) {
85 assert(!ops.empty());
86 ops.rbegin()->op.flags = flags;
87 }
88
89 class C_TwoContexts;
90 /**
91 * Add a callback to run when this operation completes,
92 * after any other callbacks for it.
93 */
94 void add_handler(Context *extra);
95
96 OSDOp& add_op(int op) {
97 int s = ops.size();
98 ops.resize(s+1);
99 ops[s].op.op = op;
100 out_bl.resize(s+1);
101 out_bl[s] = NULL;
102 out_handler.resize(s+1);
103 out_handler[s] = NULL;
104 out_rval.resize(s+1);
105 out_rval[s] = NULL;
106 return ops[s];
107 }
108 void add_data(int op, uint64_t off, uint64_t len, bufferlist& bl) {
109 OSDOp& osd_op = add_op(op);
110 osd_op.op.extent.offset = off;
111 osd_op.op.extent.length = len;
112 osd_op.indata.claim_append(bl);
113 }
114 void add_writesame(int op, uint64_t off, uint64_t write_len,
115 bufferlist& bl) {
116 OSDOp& osd_op = add_op(op);
117 osd_op.op.writesame.offset = off;
118 osd_op.op.writesame.length = write_len;
119 osd_op.op.writesame.data_length = bl.length();
120 osd_op.indata.claim_append(bl);
121 }
122 void add_xattr(int op, const char *name, const bufferlist& data) {
123 OSDOp& osd_op = add_op(op);
124 osd_op.op.xattr.name_len = (name ? strlen(name) : 0);
125 osd_op.op.xattr.value_len = data.length();
126 if (name)
127 osd_op.indata.append(name);
128 osd_op.indata.append(data);
129 }
130 void add_xattr_cmp(int op, const char *name, uint8_t cmp_op,
131 uint8_t cmp_mode, const bufferlist& data) {
132 OSDOp& osd_op = add_op(op);
133 osd_op.op.xattr.name_len = (name ? strlen(name) : 0);
134 osd_op.op.xattr.value_len = data.length();
135 osd_op.op.xattr.cmp_op = cmp_op;
136 osd_op.op.xattr.cmp_mode = cmp_mode;
137 if (name)
138 osd_op.indata.append(name);
139 osd_op.indata.append(data);
140 }
141 void add_call(int op, const char *cname, const char *method,
142 bufferlist &indata,
143 bufferlist *outbl, Context *ctx, int *prval) {
144 OSDOp& osd_op = add_op(op);
145
146 unsigned p = ops.size() - 1;
147 out_handler[p] = ctx;
148 out_bl[p] = outbl;
149 out_rval[p] = prval;
150
151 osd_op.op.cls.class_len = strlen(cname);
152 osd_op.op.cls.method_len = strlen(method);
153 osd_op.op.cls.indata_len = indata.length();
154 osd_op.indata.append(cname, osd_op.op.cls.class_len);
155 osd_op.indata.append(method, osd_op.op.cls.method_len);
156 osd_op.indata.append(indata);
157 }
158 void add_pgls(int op, uint64_t count, collection_list_handle_t cookie,
159 epoch_t start_epoch) {
160 OSDOp& osd_op = add_op(op);
161 osd_op.op.pgls.count = count;
162 osd_op.op.pgls.start_epoch = start_epoch;
163 ::encode(cookie, osd_op.indata);
164 }
165 void add_pgls_filter(int op, uint64_t count, const bufferlist& filter,
166 collection_list_handle_t cookie, epoch_t start_epoch) {
167 OSDOp& osd_op = add_op(op);
168 osd_op.op.pgls.count = count;
169 osd_op.op.pgls.start_epoch = start_epoch;
170 string cname = "pg";
171 string mname = "filter";
172 ::encode(cname, osd_op.indata);
173 ::encode(mname, osd_op.indata);
174 osd_op.indata.append(filter);
175 ::encode(cookie, osd_op.indata);
176 }
177 void add_alloc_hint(int op, uint64_t expected_object_size,
178 uint64_t expected_write_size,
179 uint32_t flags) {
180 OSDOp& osd_op = add_op(op);
181 osd_op.op.alloc_hint.expected_object_size = expected_object_size;
182 osd_op.op.alloc_hint.expected_write_size = expected_write_size;
183 osd_op.op.alloc_hint.flags = flags;
184 }
185
186 // ------
187
188 // pg
189 void pg_ls(uint64_t count, bufferlist& filter,
190 collection_list_handle_t cookie, epoch_t start_epoch) {
191 if (filter.length() == 0)
192 add_pgls(CEPH_OSD_OP_PGLS, count, cookie, start_epoch);
193 else
194 add_pgls_filter(CEPH_OSD_OP_PGLS_FILTER, count, filter, cookie,
195 start_epoch);
196 flags |= CEPH_OSD_FLAG_PGOP;
197 }
198
199 void pg_nls(uint64_t count, const bufferlist& filter,
200 collection_list_handle_t cookie, epoch_t start_epoch) {
201 if (filter.length() == 0)
202 add_pgls(CEPH_OSD_OP_PGNLS, count, cookie, start_epoch);
203 else
204 add_pgls_filter(CEPH_OSD_OP_PGNLS_FILTER, count, filter, cookie,
205 start_epoch);
206 flags |= CEPH_OSD_FLAG_PGOP;
207 }
208
209 void scrub_ls(const librados::object_id_t& start_after,
210 uint64_t max_to_get,
211 std::vector<librados::inconsistent_obj_t> *objects,
212 uint32_t *interval,
213 int *rval);
214 void scrub_ls(const librados::object_id_t& start_after,
215 uint64_t max_to_get,
216 std::vector<librados::inconsistent_snapset_t> *objects,
217 uint32_t *interval,
218 int *rval);
219
220 void create(bool excl) {
221 OSDOp& o = add_op(CEPH_OSD_OP_CREATE);
222 o.op.flags = (excl ? CEPH_OSD_OP_FLAG_EXCL : 0);
223 }
224
225 struct C_ObjectOperation_stat : public Context {
226 bufferlist bl;
227 uint64_t *psize;
228 ceph::real_time *pmtime;
229 time_t *ptime;
230 struct timespec *pts;
231 int *prval;
232 C_ObjectOperation_stat(uint64_t *ps, ceph::real_time *pm, time_t *pt, struct timespec *_pts,
233 int *prval)
234 : psize(ps), pmtime(pm), ptime(pt), pts(_pts), prval(prval) {}
235 void finish(int r) override {
236 if (r >= 0) {
237 bufferlist::iterator p = bl.begin();
238 try {
239 uint64_t size;
240 ceph::real_time mtime;
241 ::decode(size, p);
242 ::decode(mtime, p);
243 if (psize)
244 *psize = size;
245 if (pmtime)
246 *pmtime = mtime;
247 if (ptime)
248 *ptime = ceph::real_clock::to_time_t(mtime);
249 if (pts)
250 *pts = ceph::real_clock::to_timespec(mtime);
251 } catch (buffer::error& e) {
252 if (prval)
253 *prval = -EIO;
254 }
255 }
256 }
257 };
258 void stat(uint64_t *psize, ceph::real_time *pmtime, int *prval) {
259 add_op(CEPH_OSD_OP_STAT);
260 unsigned p = ops.size() - 1;
261 C_ObjectOperation_stat *h = new C_ObjectOperation_stat(psize, pmtime, NULL, NULL,
262 prval);
263 out_bl[p] = &h->bl;
264 out_handler[p] = h;
265 out_rval[p] = prval;
266 }
267 void stat(uint64_t *psize, time_t *ptime, int *prval) {
268 add_op(CEPH_OSD_OP_STAT);
269 unsigned p = ops.size() - 1;
270 C_ObjectOperation_stat *h = new C_ObjectOperation_stat(psize, NULL, ptime, NULL,
271 prval);
272 out_bl[p] = &h->bl;
273 out_handler[p] = h;
274 out_rval[p] = prval;
275 }
276 void stat(uint64_t *psize, struct timespec *pts, int *prval) {
277 add_op(CEPH_OSD_OP_STAT);
278 unsigned p = ops.size() - 1;
279 C_ObjectOperation_stat *h = new C_ObjectOperation_stat(psize, NULL, NULL, pts,
280 prval);
281 out_bl[p] = &h->bl;
282 out_handler[p] = h;
283 out_rval[p] = prval;
284 }
285 // object cmpext
286 struct C_ObjectOperation_cmpext : public Context {
287 int *prval;
288 C_ObjectOperation_cmpext(int *prval)
289 : prval(prval) {}
290
291 void finish(int r) {
292 if (prval)
293 *prval = r;
294 }
295 };
296
297 void cmpext(uint64_t off, bufferlist& cmp_bl, int *prval) {
298 add_data(CEPH_OSD_OP_CMPEXT, off, cmp_bl.length(), cmp_bl);
299 unsigned p = ops.size() - 1;
300 C_ObjectOperation_cmpext *h = new C_ObjectOperation_cmpext(prval);
301 out_handler[p] = h;
302 out_rval[p] = prval;
303 }
304
305 // Used by C API
306 void cmpext(uint64_t off, uint64_t cmp_len, const char *cmp_buf, int *prval) {
307 bufferlist cmp_bl;
308 cmp_bl.append(cmp_buf, cmp_len);
309 add_data(CEPH_OSD_OP_CMPEXT, off, cmp_len, cmp_bl);
310 unsigned p = ops.size() - 1;
311 C_ObjectOperation_cmpext *h = new C_ObjectOperation_cmpext(prval);
312 out_handler[p] = h;
313 out_rval[p] = prval;
314 }
315
316 void read(uint64_t off, uint64_t len, bufferlist *pbl, int *prval,
317 Context* ctx) {
318 bufferlist bl;
319 add_data(CEPH_OSD_OP_READ, off, len, bl);
320 unsigned p = ops.size() - 1;
321 out_bl[p] = pbl;
322 out_rval[p] = prval;
323 out_handler[p] = ctx;
324 }
325
326 struct C_ObjectOperation_sparse_read : public Context {
327 bufferlist bl;
328 bufferlist *data_bl;
329 std::map<uint64_t, uint64_t> *extents;
330 int *prval;
331 C_ObjectOperation_sparse_read(bufferlist *data_bl,
332 std::map<uint64_t, uint64_t> *extents,
333 int *prval)
334 : data_bl(data_bl), extents(extents), prval(prval) {}
335 void finish(int r) override {
336 bufferlist::iterator iter = bl.begin();
337 if (r >= 0) {
338 try {
339 ::decode(*extents, iter);
340 ::decode(*data_bl, iter);
341 } catch (buffer::error& e) {
342 if (prval)
343 *prval = -EIO;
344 }
345 }
346 }
347 };
348 void sparse_read(uint64_t off, uint64_t len, std::map<uint64_t,uint64_t> *m,
349 bufferlist *data_bl, int *prval) {
350 bufferlist bl;
351 add_data(CEPH_OSD_OP_SPARSE_READ, off, len, bl);
352 unsigned p = ops.size() - 1;
353 C_ObjectOperation_sparse_read *h =
354 new C_ObjectOperation_sparse_read(data_bl, m, prval);
355 out_bl[p] = &h->bl;
356 out_handler[p] = h;
357 out_rval[p] = prval;
358 }
359 void write(uint64_t off, bufferlist& bl,
360 uint64_t truncate_size,
361 uint32_t truncate_seq) {
362 add_data(CEPH_OSD_OP_WRITE, off, bl.length(), bl);
363 OSDOp& o = *ops.rbegin();
364 o.op.extent.truncate_size = truncate_size;
365 o.op.extent.truncate_seq = truncate_seq;
366 }
367 void write(uint64_t off, bufferlist& bl) {
368 write(off, bl, 0, 0);
369 }
370 void write_full(bufferlist& bl) {
371 add_data(CEPH_OSD_OP_WRITEFULL, 0, bl.length(), bl);
372 }
373 void writesame(uint64_t off, uint64_t write_len, bufferlist& bl) {
374 add_writesame(CEPH_OSD_OP_WRITESAME, off, write_len, bl);
375 }
376 void append(bufferlist& bl) {
377 add_data(CEPH_OSD_OP_APPEND, 0, bl.length(), bl);
378 }
379 void zero(uint64_t off, uint64_t len) {
380 bufferlist bl;
381 add_data(CEPH_OSD_OP_ZERO, off, len, bl);
382 }
383 void truncate(uint64_t off) {
384 bufferlist bl;
385 add_data(CEPH_OSD_OP_TRUNCATE, off, 0, bl);
386 }
387 void remove() {
388 bufferlist bl;
389 add_data(CEPH_OSD_OP_DELETE, 0, 0, bl);
390 }
391 void mapext(uint64_t off, uint64_t len) {
392 bufferlist bl;
393 add_data(CEPH_OSD_OP_MAPEXT, off, len, bl);
394 }
395 void sparse_read(uint64_t off, uint64_t len) {
396 bufferlist bl;
397 add_data(CEPH_OSD_OP_SPARSE_READ, off, len, bl);
398 }
399
400 void checksum(uint8_t type, const bufferlist &init_value_bl,
401 uint64_t off, uint64_t len, size_t chunk_size,
402 bufferlist *pbl, int *prval, Context *ctx) {
403 OSDOp& osd_op = add_op(CEPH_OSD_OP_CHECKSUM);
404 osd_op.op.checksum.offset = off;
405 osd_op.op.checksum.length = len;
406 osd_op.op.checksum.type = type;
407 osd_op.op.checksum.chunk_size = chunk_size;
408 osd_op.indata.append(init_value_bl);
409
410 unsigned p = ops.size() - 1;
411 out_bl[p] = pbl;
412 out_rval[p] = prval;
413 out_handler[p] = ctx;
414 }
415
416 // object attrs
417 void getxattr(const char *name, bufferlist *pbl, int *prval) {
418 bufferlist bl;
419 add_xattr(CEPH_OSD_OP_GETXATTR, name, bl);
420 unsigned p = ops.size() - 1;
421 out_bl[p] = pbl;
422 out_rval[p] = prval;
423 }
424 struct C_ObjectOperation_decodevals : public Context {
425 uint64_t max_entries;
426 bufferlist bl;
427 std::map<std::string,bufferlist> *pattrs;
428 bool *ptruncated;
429 int *prval;
430 C_ObjectOperation_decodevals(uint64_t m, std::map<std::string,bufferlist> *pa,
431 bool *pt, int *pr)
432 : max_entries(m), pattrs(pa), ptruncated(pt), prval(pr) {
433 if (ptruncated) {
434 *ptruncated = false;
435 }
436 }
437 void finish(int r) override {
438 if (r >= 0) {
439 bufferlist::iterator p = bl.begin();
440 try {
441 if (pattrs)
442 ::decode(*pattrs, p);
443 if (ptruncated) {
444 std::map<std::string,bufferlist> ignore;
445 if (!pattrs) {
446 ::decode(ignore, p);
447 pattrs = &ignore;
448 }
449 if (!p.end()) {
450 ::decode(*ptruncated, p);
451 } else {
452 // the OSD did not provide this. since old OSDs do not
453 // enfoce omap result limits either, we can infer it from
454 // the size of the result
455 *ptruncated = (pattrs->size() == max_entries);
456 }
457 }
458 }
459 catch (buffer::error& e) {
460 if (prval)
461 *prval = -EIO;
462 }
463 }
464 }
465 };
466 struct C_ObjectOperation_decodekeys : public Context {
467 uint64_t max_entries;
468 bufferlist bl;
469 std::set<std::string> *pattrs;
470 bool *ptruncated;
471 int *prval;
472 C_ObjectOperation_decodekeys(uint64_t m, std::set<std::string> *pa, bool *pt,
473 int *pr)
474 : max_entries(m), pattrs(pa), ptruncated(pt), prval(pr) {
475 if (ptruncated) {
476 *ptruncated = false;
477 }
478 }
479 void finish(int r) override {
480 if (r >= 0) {
481 bufferlist::iterator p = bl.begin();
482 try {
483 if (pattrs)
484 ::decode(*pattrs, p);
485 if (ptruncated) {
486 std::set<std::string> ignore;
487 if (!pattrs) {
488 ::decode(ignore, p);
489 pattrs = &ignore;
490 }
491 if (!p.end()) {
492 ::decode(*ptruncated, p);
493 } else {
494 // the OSD did not provide this. since old OSDs do not
495 // enfoce omap result limits either, we can infer it from
496 // the size of the result
497 *ptruncated = (pattrs->size() == max_entries);
498 }
499 }
500 }
501 catch (buffer::error& e) {
502 if (prval)
503 *prval = -EIO;
504 }
505 }
506 }
507 };
508 struct C_ObjectOperation_decodewatchers : public Context {
509 bufferlist bl;
510 list<obj_watch_t> *pwatchers;
511 int *prval;
512 C_ObjectOperation_decodewatchers(list<obj_watch_t> *pw, int *pr)
513 : pwatchers(pw), prval(pr) {}
514 void finish(int r) override {
515 if (r >= 0) {
516 bufferlist::iterator p = bl.begin();
517 try {
518 obj_list_watch_response_t resp;
519 ::decode(resp, p);
520 if (pwatchers) {
521 for (list<watch_item_t>::iterator i = resp.entries.begin() ;
522 i != resp.entries.end() ; ++i) {
523 obj_watch_t ow;
524 ostringstream sa;
525 sa << i->addr;
526 strncpy(ow.addr, sa.str().c_str(), 256);
527 ow.watcher_id = i->name.num();
528 ow.cookie = i->cookie;
529 ow.timeout_seconds = i->timeout_seconds;
530 pwatchers->push_back(ow);
531 }
532 }
533 }
534 catch (buffer::error& e) {
535 if (prval)
536 *prval = -EIO;
537 }
538 }
539 }
540 };
541 struct C_ObjectOperation_decodesnaps : public Context {
542 bufferlist bl;
543 librados::snap_set_t *psnaps;
544 int *prval;
545 C_ObjectOperation_decodesnaps(librados::snap_set_t *ps, int *pr)
546 : psnaps(ps), prval(pr) {}
547 void finish(int r) override {
548 if (r >= 0) {
549 bufferlist::iterator p = bl.begin();
550 try {
551 obj_list_snap_response_t resp;
552 ::decode(resp, p);
553 if (psnaps) {
554 psnaps->clones.clear();
555 for (vector<clone_info>::iterator ci = resp.clones.begin();
556 ci != resp.clones.end();
557 ++ci) {
558 librados::clone_info_t clone;
559
560 clone.cloneid = ci->cloneid;
561 clone.snaps.reserve(ci->snaps.size());
562 clone.snaps.insert(clone.snaps.end(), ci->snaps.begin(),
563 ci->snaps.end());
564 clone.overlap = ci->overlap;
565 clone.size = ci->size;
566
567 psnaps->clones.push_back(clone);
568 }
569 psnaps->seq = resp.seq;
570 }
571 } catch (buffer::error& e) {
572 if (prval)
573 *prval = -EIO;
574 }
575 }
576 }
577 };
578 void getxattrs(std::map<std::string,bufferlist> *pattrs, int *prval) {
579 add_op(CEPH_OSD_OP_GETXATTRS);
580 if (pattrs || prval) {
581 unsigned p = ops.size() - 1;
582 C_ObjectOperation_decodevals *h
583 = new C_ObjectOperation_decodevals(0, pattrs, nullptr, prval);
584 out_handler[p] = h;
585 out_bl[p] = &h->bl;
586 out_rval[p] = prval;
587 }
588 }
589 void setxattr(const char *name, const bufferlist& bl) {
590 add_xattr(CEPH_OSD_OP_SETXATTR, name, bl);
591 }
592 void setxattr(const char *name, const string& s) {
593 bufferlist bl;
594 bl.append(s);
595 add_xattr(CEPH_OSD_OP_SETXATTR, name, bl);
596 }
597 void cmpxattr(const char *name, uint8_t cmp_op, uint8_t cmp_mode,
598 const bufferlist& bl) {
599 add_xattr_cmp(CEPH_OSD_OP_CMPXATTR, name, cmp_op, cmp_mode, bl);
600 }
601 void rmxattr(const char *name) {
602 bufferlist bl;
603 add_xattr(CEPH_OSD_OP_RMXATTR, name, bl);
604 }
605 void setxattrs(map<string, bufferlist>& attrs) {
606 bufferlist bl;
607 ::encode(attrs, bl);
608 add_xattr(CEPH_OSD_OP_RESETXATTRS, 0, bl.length());
609 }
610 void resetxattrs(const char *prefix, map<string, bufferlist>& attrs) {
611 bufferlist bl;
612 ::encode(attrs, bl);
613 add_xattr(CEPH_OSD_OP_RESETXATTRS, prefix, bl);
614 }
615
616 // trivialmap
617 void tmap_update(bufferlist& bl) {
618 add_data(CEPH_OSD_OP_TMAPUP, 0, 0, bl);
619 }
620 void tmap_put(bufferlist& bl) {
621 add_data(CEPH_OSD_OP_TMAPPUT, 0, bl.length(), bl);
622 }
623 void tmap_get(bufferlist *pbl, int *prval) {
624 add_op(CEPH_OSD_OP_TMAPGET);
625 unsigned p = ops.size() - 1;
626 out_bl[p] = pbl;
627 out_rval[p] = prval;
628 }
629 void tmap_get() {
630 add_op(CEPH_OSD_OP_TMAPGET);
631 }
632 void tmap_to_omap(bool nullok=false) {
633 OSDOp& osd_op = add_op(CEPH_OSD_OP_TMAP2OMAP);
634 if (nullok)
635 osd_op.op.tmap2omap.flags = CEPH_OSD_TMAP2OMAP_NULLOK;
636 }
637
638 // objectmap
639 void omap_get_keys(const string &start_after,
640 uint64_t max_to_get,
641 std::set<std::string> *out_set,
642 bool *ptruncated,
643 int *prval) {
644 OSDOp &op = add_op(CEPH_OSD_OP_OMAPGETKEYS);
645 bufferlist bl;
646 ::encode(start_after, bl);
647 ::encode(max_to_get, bl);
648 op.op.extent.offset = 0;
649 op.op.extent.length = bl.length();
650 op.indata.claim_append(bl);
651 if (prval || ptruncated || out_set) {
652 unsigned p = ops.size() - 1;
653 C_ObjectOperation_decodekeys *h =
654 new C_ObjectOperation_decodekeys(max_to_get, out_set, ptruncated, prval);
655 out_handler[p] = h;
656 out_bl[p] = &h->bl;
657 out_rval[p] = prval;
658 }
659 }
660
661 void omap_get_vals(const string &start_after,
662 const string &filter_prefix,
663 uint64_t max_to_get,
664 std::map<std::string, bufferlist> *out_set,
665 bool *ptruncated,
666 int *prval) {
667 OSDOp &op = add_op(CEPH_OSD_OP_OMAPGETVALS);
668 bufferlist bl;
669 ::encode(start_after, bl);
670 ::encode(max_to_get, bl);
671 ::encode(filter_prefix, bl);
672 op.op.extent.offset = 0;
673 op.op.extent.length = bl.length();
674 op.indata.claim_append(bl);
675 if (prval || out_set || ptruncated) {
676 unsigned p = ops.size() - 1;
677 C_ObjectOperation_decodevals *h =
678 new C_ObjectOperation_decodevals(max_to_get, out_set, ptruncated, prval);
679 out_handler[p] = h;
680 out_bl[p] = &h->bl;
681 out_rval[p] = prval;
682 }
683 }
684
685 void omap_get_vals_by_keys(const std::set<std::string> &to_get,
686 std::map<std::string, bufferlist> *out_set,
687 int *prval) {
688 OSDOp &op = add_op(CEPH_OSD_OP_OMAPGETVALSBYKEYS);
689 bufferlist bl;
690 ::encode(to_get, bl);
691 op.op.extent.offset = 0;
692 op.op.extent.length = bl.length();
693 op.indata.claim_append(bl);
694 if (prval || out_set) {
695 unsigned p = ops.size() - 1;
696 C_ObjectOperation_decodevals *h =
697 new C_ObjectOperation_decodevals(0, out_set, nullptr, prval);
698 out_handler[p] = h;
699 out_bl[p] = &h->bl;
700 out_rval[p] = prval;
701 }
702 }
703
704 void omap_cmp(const std::map<std::string, pair<bufferlist,int> > &assertions,
705 int *prval) {
706 OSDOp &op = add_op(CEPH_OSD_OP_OMAP_CMP);
707 bufferlist bl;
708 ::encode(assertions, bl);
709 op.op.extent.offset = 0;
710 op.op.extent.length = bl.length();
711 op.indata.claim_append(bl);
712 if (prval) {
713 unsigned p = ops.size() - 1;
714 out_rval[p] = prval;
715 }
716 }
717
718 struct C_ObjectOperation_copyget : public Context {
719 bufferlist bl;
720 object_copy_cursor_t *cursor;
721 uint64_t *out_size;
722 ceph::real_time *out_mtime;
723 std::map<std::string,bufferlist> *out_attrs;
724 bufferlist *out_data, *out_omap_header, *out_omap_data;
725 vector<snapid_t> *out_snaps;
726 snapid_t *out_snap_seq;
727 uint32_t *out_flags;
728 uint32_t *out_data_digest;
729 uint32_t *out_omap_digest;
31f18b77 730 mempool::osd_pglog::vector<pair<osd_reqid_t, version_t> > *out_reqids;
7c673cae
FG
731 uint64_t *out_truncate_seq;
732 uint64_t *out_truncate_size;
733 int *prval;
734 C_ObjectOperation_copyget(object_copy_cursor_t *c,
735 uint64_t *s,
736 ceph::real_time *m,
737 std::map<std::string,bufferlist> *a,
738 bufferlist *d, bufferlist *oh,
739 bufferlist *o,
740 std::vector<snapid_t> *osnaps,
741 snapid_t *osnap_seq,
742 uint32_t *flags,
743 uint32_t *dd,
744 uint32_t *od,
31f18b77 745 mempool::osd_pglog::vector<pair<osd_reqid_t, version_t> > *oreqids,
7c673cae
FG
746 uint64_t *otseq,
747 uint64_t *otsize,
748 int *r)
749 : cursor(c),
750 out_size(s), out_mtime(m),
751 out_attrs(a), out_data(d), out_omap_header(oh),
752 out_omap_data(o), out_snaps(osnaps), out_snap_seq(osnap_seq),
753 out_flags(flags), out_data_digest(dd), out_omap_digest(od),
754 out_reqids(oreqids),
755 out_truncate_seq(otseq),
756 out_truncate_size(otsize),
757 prval(r) {}
758 void finish(int r) override {
759 // reqids are copied on ENOENT
760 if (r < 0 && r != -ENOENT)
761 return;
762 try {
763 bufferlist::iterator p = bl.begin();
764 object_copy_data_t copy_reply;
765 ::decode(copy_reply, p);
766 if (r == -ENOENT) {
767 if (out_reqids)
768 *out_reqids = copy_reply.reqids;
769 return;
770 }
771 if (out_size)
772 *out_size = copy_reply.size;
773 if (out_mtime)
774 *out_mtime = ceph::real_clock::from_ceph_timespec(copy_reply.mtime);
775 if (out_attrs)
776 *out_attrs = copy_reply.attrs;
777 if (out_data)
778 out_data->claim_append(copy_reply.data);
779 if (out_omap_header)
780 out_omap_header->claim_append(copy_reply.omap_header);
781 if (out_omap_data)
782 *out_omap_data = copy_reply.omap_data;
783 if (out_snaps)
784 *out_snaps = copy_reply.snaps;
785 if (out_snap_seq)
786 *out_snap_seq = copy_reply.snap_seq;
787 if (out_flags)
788 *out_flags = copy_reply.flags;
789 if (out_data_digest)
790 *out_data_digest = copy_reply.data_digest;
791 if (out_omap_digest)
792 *out_omap_digest = copy_reply.omap_digest;
793 if (out_reqids)
794 *out_reqids = copy_reply.reqids;
795 if (out_truncate_seq)
796 *out_truncate_seq = copy_reply.truncate_seq;
797 if (out_truncate_size)
798 *out_truncate_size = copy_reply.truncate_size;
799 *cursor = copy_reply.cursor;
800 } catch (buffer::error& e) {
801 if (prval)
802 *prval = -EIO;
803 }
804 }
805 };
806
807 void copy_get(object_copy_cursor_t *cursor,
808 uint64_t max,
809 uint64_t *out_size,
810 ceph::real_time *out_mtime,
811 std::map<std::string,bufferlist> *out_attrs,
812 bufferlist *out_data,
813 bufferlist *out_omap_header,
814 bufferlist *out_omap_data,
815 vector<snapid_t> *out_snaps,
816 snapid_t *out_snap_seq,
817 uint32_t *out_flags,
818 uint32_t *out_data_digest,
819 uint32_t *out_omap_digest,
31f18b77 820 mempool::osd_pglog::vector<pair<osd_reqid_t, version_t> > *out_reqids,
7c673cae
FG
821 uint64_t *truncate_seq,
822 uint64_t *truncate_size,
823 int *prval) {
824 OSDOp& osd_op = add_op(CEPH_OSD_OP_COPY_GET);
825 osd_op.op.copy_get.max = max;
826 ::encode(*cursor, osd_op.indata);
827 ::encode(max, osd_op.indata);
828 unsigned p = ops.size() - 1;
829 out_rval[p] = prval;
830 C_ObjectOperation_copyget *h =
831 new C_ObjectOperation_copyget(cursor, out_size, out_mtime,
832 out_attrs, out_data, out_omap_header,
833 out_omap_data, out_snaps, out_snap_seq,
834 out_flags, out_data_digest,
835 out_omap_digest, out_reqids, truncate_seq,
836 truncate_size, prval);
837 out_bl[p] = &h->bl;
838 out_handler[p] = h;
839 }
840
841 void undirty() {
842 add_op(CEPH_OSD_OP_UNDIRTY);
843 }
844
845 struct C_ObjectOperation_isdirty : public Context {
846 bufferlist bl;
847 bool *pisdirty;
848 int *prval;
849 C_ObjectOperation_isdirty(bool *p, int *r)
850 : pisdirty(p), prval(r) {}
851 void finish(int r) override {
852 if (r < 0)
853 return;
854 try {
855 bufferlist::iterator p = bl.begin();
856 bool isdirty;
857 ::decode(isdirty, p);
858 if (pisdirty)
859 *pisdirty = isdirty;
860 } catch (buffer::error& e) {
861 if (prval)
862 *prval = -EIO;
863 }
864 }
865 };
866
867 void is_dirty(bool *pisdirty, int *prval) {
868 add_op(CEPH_OSD_OP_ISDIRTY);
869 unsigned p = ops.size() - 1;
870 out_rval[p] = prval;
871 C_ObjectOperation_isdirty *h =
872 new C_ObjectOperation_isdirty(pisdirty, prval);
873 out_bl[p] = &h->bl;
874 out_handler[p] = h;
875 }
876
877 struct C_ObjectOperation_hit_set_ls : public Context {
878 bufferlist bl;
879 std::list< std::pair<time_t, time_t> > *ptls;
880 std::list< std::pair<ceph::real_time, ceph::real_time> > *putls;
881 int *prval;
882 C_ObjectOperation_hit_set_ls(std::list< std::pair<time_t, time_t> > *t,
883 std::list< std::pair<ceph::real_time,
884 ceph::real_time> > *ut,
885 int *r)
886 : ptls(t), putls(ut), prval(r) {}
887 void finish(int r) override {
888 if (r < 0)
889 return;
890 try {
891 bufferlist::iterator p = bl.begin();
892 std::list< std::pair<ceph::real_time, ceph::real_time> > ls;
893 ::decode(ls, p);
894 if (ptls) {
895 ptls->clear();
896 for (auto p = ls.begin(); p != ls.end(); ++p)
897 // round initial timestamp up to the next full second to
898 // keep this a valid interval.
899 ptls->push_back(
900 make_pair(ceph::real_clock::to_time_t(
901 ceph::ceil(p->first,
902 // Sadly, no time literals until C++14.
903 std::chrono::seconds(1))),
904 ceph::real_clock::to_time_t(p->second)));
905 }
906 if (putls)
907 putls->swap(ls);
908 } catch (buffer::error& e) {
909 r = -EIO;
910 }
911 if (prval)
912 *prval = r;
913 }
914 };
915
916 /**
917 * list available HitSets.
918 *
919 * We will get back a list of time intervals. Note that the most
920 * recent range may have an empty end timestamp if it is still
921 * accumulating.
922 *
923 * @param pls [out] list of time intervals
924 * @param prval [out] return value
925 */
926 void hit_set_ls(std::list< std::pair<time_t, time_t> > *pls, int *prval) {
927 add_op(CEPH_OSD_OP_PG_HITSET_LS);
928 unsigned p = ops.size() - 1;
929 out_rval[p] = prval;
930 C_ObjectOperation_hit_set_ls *h =
931 new C_ObjectOperation_hit_set_ls(pls, NULL, prval);
932 out_bl[p] = &h->bl;
933 out_handler[p] = h;
934 }
935 void hit_set_ls(std::list<std::pair<ceph::real_time, ceph::real_time> > *pls,
936 int *prval) {
937 add_op(CEPH_OSD_OP_PG_HITSET_LS);
938 unsigned p = ops.size() - 1;
939 out_rval[p] = prval;
940 C_ObjectOperation_hit_set_ls *h =
941 new C_ObjectOperation_hit_set_ls(NULL, pls, prval);
942 out_bl[p] = &h->bl;
943 out_handler[p] = h;
944 }
945
946 /**
947 * get HitSet
948 *
949 * Return an encoded HitSet that includes the provided time
950 * interval.
951 *
952 * @param stamp [in] timestamp
953 * @param pbl [out] target buffer for encoded HitSet
954 * @param prval [out] return value
955 */
956 void hit_set_get(ceph::real_time stamp, bufferlist *pbl, int *prval) {
957 OSDOp& op = add_op(CEPH_OSD_OP_PG_HITSET_GET);
958 op.op.hit_set_get.stamp = ceph::real_clock::to_ceph_timespec(stamp);
959 unsigned p = ops.size() - 1;
960 out_rval[p] = prval;
961 out_bl[p] = pbl;
962 }
963
964 void omap_get_header(bufferlist *bl, int *prval) {
965 add_op(CEPH_OSD_OP_OMAPGETHEADER);
966 unsigned p = ops.size() - 1;
967 out_bl[p] = bl;
968 out_rval[p] = prval;
969 }
970
971 void omap_set(const map<string, bufferlist> &map) {
972 bufferlist bl;
973 ::encode(map, bl);
974 add_data(CEPH_OSD_OP_OMAPSETVALS, 0, bl.length(), bl);
975 }
976
977 void omap_set_header(bufferlist &bl) {
978 add_data(CEPH_OSD_OP_OMAPSETHEADER, 0, bl.length(), bl);
979 }
980
981 void omap_clear() {
982 add_op(CEPH_OSD_OP_OMAPCLEAR);
983 }
984
985 void omap_rm_keys(const std::set<std::string> &to_remove) {
986 bufferlist bl;
987 ::encode(to_remove, bl);
988 add_data(CEPH_OSD_OP_OMAPRMKEYS, 0, bl.length(), bl);
989 }
990
991 // object classes
992 void call(const char *cname, const char *method, bufferlist &indata) {
993 add_call(CEPH_OSD_OP_CALL, cname, method, indata, NULL, NULL, NULL);
994 }
995
996 void call(const char *cname, const char *method, bufferlist &indata,
997 bufferlist *outdata, Context *ctx, int *prval) {
998 add_call(CEPH_OSD_OP_CALL, cname, method, indata, outdata, ctx, prval);
999 }
1000
1001 // watch/notify
1002 void watch(uint64_t cookie, __u8 op, uint32_t timeout = 0) {
1003 OSDOp& osd_op = add_op(CEPH_OSD_OP_WATCH);
1004 osd_op.op.watch.cookie = cookie;
1005 osd_op.op.watch.op = op;
1006 osd_op.op.watch.timeout = timeout;
1007 }
1008
1009 void notify(uint64_t cookie, uint32_t prot_ver, uint32_t timeout,
1010 bufferlist &bl, bufferlist *inbl) {
1011 OSDOp& osd_op = add_op(CEPH_OSD_OP_NOTIFY);
1012 osd_op.op.notify.cookie = cookie;
1013 ::encode(prot_ver, *inbl);
1014 ::encode(timeout, *inbl);
1015 ::encode(bl, *inbl);
1016 osd_op.indata.append(*inbl);
1017 }
1018
1019 void notify_ack(uint64_t notify_id, uint64_t cookie,
1020 bufferlist& reply_bl) {
1021 OSDOp& osd_op = add_op(CEPH_OSD_OP_NOTIFY_ACK);
1022 bufferlist bl;
1023 ::encode(notify_id, bl);
1024 ::encode(cookie, bl);
1025 ::encode(reply_bl, bl);
1026 osd_op.indata.append(bl);
1027 }
1028
1029 void list_watchers(list<obj_watch_t> *out,
1030 int *prval) {
1031 (void)add_op(CEPH_OSD_OP_LIST_WATCHERS);
1032 if (prval || out) {
1033 unsigned p = ops.size() - 1;
1034 C_ObjectOperation_decodewatchers *h =
1035 new C_ObjectOperation_decodewatchers(out, prval);
1036 out_handler[p] = h;
1037 out_bl[p] = &h->bl;
1038 out_rval[p] = prval;
1039 }
1040 }
1041
1042 void list_snaps(librados::snap_set_t *out, int *prval) {
1043 (void)add_op(CEPH_OSD_OP_LIST_SNAPS);
1044 if (prval || out) {
1045 unsigned p = ops.size() - 1;
1046 C_ObjectOperation_decodesnaps *h =
1047 new C_ObjectOperation_decodesnaps(out, prval);
1048 out_handler[p] = h;
1049 out_bl[p] = &h->bl;
1050 out_rval[p] = prval;
1051 }
1052 }
1053
1054 void assert_version(uint64_t ver) {
1055 OSDOp& osd_op = add_op(CEPH_OSD_OP_ASSERT_VER);
1056 osd_op.op.assert_ver.ver = ver;
1057 }
1058
1059 void cmpxattr(const char *name, const bufferlist& val,
1060 int op, int mode) {
1061 add_xattr(CEPH_OSD_OP_CMPXATTR, name, val);
1062 OSDOp& o = *ops.rbegin();
1063 o.op.xattr.cmp_op = op;
1064 o.op.xattr.cmp_mode = mode;
1065 }
1066
1067 void rollback(uint64_t snapid) {
1068 OSDOp& osd_op = add_op(CEPH_OSD_OP_ROLLBACK);
1069 osd_op.op.snap.snapid = snapid;
1070 }
1071
1072 void copy_from(object_t src, snapid_t snapid, object_locator_t src_oloc,
1073 version_t src_version, unsigned flags,
1074 unsigned src_fadvise_flags) {
1075 OSDOp& osd_op = add_op(CEPH_OSD_OP_COPY_FROM);
1076 osd_op.op.copy_from.snapid = snapid;
1077 osd_op.op.copy_from.src_version = src_version;
1078 osd_op.op.copy_from.flags = flags;
1079 osd_op.op.copy_from.src_fadvise_flags = src_fadvise_flags;
1080 ::encode(src, osd_op.indata);
1081 ::encode(src_oloc, osd_op.indata);
1082 }
1083
1084 /**
1085 * writeback content to backing tier
1086 *
1087 * If object is marked dirty in the cache tier, write back content
1088 * to backing tier. If the object is clean this is a no-op.
1089 *
1090 * If writeback races with an update, the update will block.
1091 *
1092 * use with IGNORE_CACHE to avoid triggering promote.
1093 */
1094 void cache_flush() {
1095 add_op(CEPH_OSD_OP_CACHE_FLUSH);
1096 }
1097
1098 /**
1099 * writeback content to backing tier
1100 *
1101 * If object is marked dirty in the cache tier, write back content
1102 * to backing tier. If the object is clean this is a no-op.
1103 *
1104 * If writeback races with an update, return EAGAIN. Requires that
1105 * the SKIPRWLOCKS flag be set.
1106 *
1107 * use with IGNORE_CACHE to avoid triggering promote.
1108 */
1109 void cache_try_flush() {
1110 add_op(CEPH_OSD_OP_CACHE_TRY_FLUSH);
1111 }
1112
1113 /**
1114 * evict object from cache tier
1115 *
1116 * If object is marked clean, remove the object from the cache tier.
1117 * Otherwise, return EBUSY.
1118 *
1119 * use with IGNORE_CACHE to avoid triggering promote.
1120 */
1121 void cache_evict() {
1122 add_op(CEPH_OSD_OP_CACHE_EVICT);
1123 }
1124
31f18b77
FG
1125 /*
1126 * Extensible tier
1127 */
1128 void set_redirect(object_t tgt, snapid_t snapid, object_locator_t tgt_oloc,
1129 version_t tgt_version) {
1130 OSDOp& osd_op = add_op(CEPH_OSD_OP_SET_REDIRECT);
1131 osd_op.op.copy_from.snapid = snapid;
1132 osd_op.op.copy_from.src_version = tgt_version;
1133 ::encode(tgt, osd_op.indata);
1134 ::encode(tgt_oloc, osd_op.indata);
1135 }
1136
7c673cae
FG
1137 void set_alloc_hint(uint64_t expected_object_size,
1138 uint64_t expected_write_size,
1139 uint32_t flags) {
1140 add_alloc_hint(CEPH_OSD_OP_SETALLOCHINT, expected_object_size,
1141 expected_write_size, flags);
1142
1143 // CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
1144 // not worth a feature bit. Set FAILOK per-op flag to make
1145 // sure older osds don't trip over an unsupported opcode.
1146 set_last_op_flags(CEPH_OSD_OP_FLAG_FAILOK);
1147 }
1148
1149 void dup(vector<OSDOp>& sops) {
1150 ops = sops;
1151 out_bl.resize(sops.size());
1152 out_handler.resize(sops.size());
1153 out_rval.resize(sops.size());
1154 for (uint32_t i = 0; i < sops.size(); i++) {
1155 out_bl[i] = &sops[i].outdata;
1156 out_handler[i] = NULL;
1157 out_rval[i] = &sops[i].rval;
1158 }
1159 }
1160
1161 /**
1162 * Pin/unpin an object in cache tier
1163 */
1164 void cache_pin() {
1165 add_op(CEPH_OSD_OP_CACHE_PIN);
1166 }
1167
1168 void cache_unpin() {
1169 add_op(CEPH_OSD_OP_CACHE_UNPIN);
1170 }
1171};
1172
1173
1174// ----------------
1175
1176
1177class Objecter : public md_config_obs_t, public Dispatcher {
1178public:
1179 // config observer bits
1180 const char** get_tracked_conf_keys() const override;
1181 void handle_conf_change(const struct md_config_t *conf,
1182 const std::set <std::string> &changed) override;
1183
1184public:
1185 Messenger *messenger;
1186 MonClient *monc;
1187 Finisher *finisher;
1188 ZTracer::Endpoint trace_endpoint;
1189private:
1190 OSDMap *osdmap;
1191public:
1192 using Dispatcher::cct;
1193 std::multimap<string,string> crush_location;
1194
31f18b77 1195 std::atomic<bool> initialized{false};
7c673cae
FG
1196
1197private:
31f18b77
FG
1198 std::atomic<uint64_t> last_tid{0};
1199 std::atomic<unsigned> inflight_ops{0};
1200 std::atomic<int> client_inc{-1};
7c673cae 1201 uint64_t max_linger_id;
31f18b77
FG
1202 std::atomic<unsigned> num_in_flight{0};
1203 std::atomic<int> global_op_flags{0}; // flags which are applied to each IO op
7c673cae
FG
1204 bool keep_balanced_budget;
1205 bool honor_osdmap_full;
1206 bool osdmap_full_try;
1207
31f18b77
FG
1208 // If this is true, accumulate a set of blacklisted entities
1209 // to be drained by consume_blacklist_events.
1210 bool blacklist_events_enabled;
1211 std::set<entity_addr_t> blacklist_events;
1212
7c673cae
FG
1213public:
1214 void maybe_request_map();
31f18b77
FG
1215
1216 void enable_blacklist_events();
7c673cae
FG
1217private:
1218
1219 void _maybe_request_map();
1220
1221 version_t last_seen_osdmap_version;
1222 version_t last_seen_pgmap_version;
1223
1224 mutable boost::shared_mutex rwlock;
1225 using lock_guard = std::unique_lock<decltype(rwlock)>;
1226 using unique_lock = std::unique_lock<decltype(rwlock)>;
1227 using shared_lock = boost::shared_lock<decltype(rwlock)>;
1228 using shunique_lock = ceph::shunique_lock<decltype(rwlock)>;
1229 ceph::timer<ceph::mono_clock> timer;
1230
1231 PerfCounters *logger;
1232
1233 uint64_t tick_event;
1234
1235 void start_tick();
1236 void tick();
1237 void update_crush_location();
1238
1239 class RequestStateHook;
1240
1241 RequestStateHook *m_request_state_hook;
1242
1243public:
1244 /*** track pending operations ***/
1245 // read
1246 public:
1247
1248 struct OSDSession;
1249
1250 struct op_target_t {
1251 int flags = 0;
1252
1253 epoch_t epoch = 0; ///< latest epoch we calculated the mapping
1254
1255 object_t base_oid;
1256 object_locator_t base_oloc;
1257 object_t target_oid;
1258 object_locator_t target_oloc;
1259
1260 ///< true if we are directed at base_pgid, not base_oid
1261 bool precalc_pgid = false;
1262
1263 ///< true if we have ever mapped to a valid pool
1264 bool pool_ever_existed = false;
1265
1266 ///< explcit pg target, if any
1267 pg_t base_pgid;
1268
1269 pg_t pgid; ///< last (raw) pg we mapped to
1270 spg_t actual_pgid; ///< last (actual) spg_t we mapped to
1271 unsigned pg_num = 0; ///< last pg_num we mapped to
1272 unsigned pg_num_mask = 0; ///< last pg_num_mask we mapped to
1273 vector<int> up; ///< set of up osds for last pg we mapped to
1274 vector<int> acting; ///< set of acting osds for last pg we mapped to
1275 int up_primary = -1; ///< last up_primary we mapped to
1276 int acting_primary = -1; ///< last acting_primary we mapped to
1277 int size = -1; ///< the size of the pool when were were last mapped
1278 int min_size = -1; ///< the min size of the pool when were were last mapped
1279 bool sort_bitwise = false; ///< whether the hobject_t sort order is bitwise
1280
1281 bool used_replica = false;
1282 bool paused = false;
1283
1284 int osd = -1; ///< the final target osd, or -1
1285
1286 epoch_t last_force_resend = 0;
1287
1288 op_target_t(object_t oid, object_locator_t oloc, int flags)
1289 : flags(flags),
1290 base_oid(oid),
1291 base_oloc(oloc)
1292 {}
1293
1294 op_target_t(pg_t pgid)
1295 : base_oloc(pgid.pool(), pgid.ps()),
1296 precalc_pgid(true),
1297 base_pgid(pgid)
1298 {}
1299
1300 op_target_t() = default;
1301
1302 hobject_t get_hobj() {
1303 return hobject_t(target_oid,
1304 target_oloc.key,
1305 CEPH_NOSNAP,
1306 target_oloc.hash >= 0 ? target_oloc.hash : pgid.ps(),
1307 target_oloc.pool,
1308 target_oloc.nspace);
1309 }
1310
1311 bool contained_by(const hobject_t& begin, const hobject_t& end) {
1312 hobject_t h = get_hobj();
1313 int r = cmp(h, begin);
1314 return r == 0 || (r > 0 && h < end);
1315 }
1316
1317 void dump(Formatter *f) const;
1318 };
1319
1320 struct Op : public RefCountedObject {
1321 OSDSession *session;
1322 int incarnation;
1323
1324 op_target_t target;
1325
1326 ConnectionRef con; // for rx buffer only
1327 uint64_t features; // explicitly specified op features
1328
1329 vector<OSDOp> ops;
1330
1331 snapid_t snapid;
1332 SnapContext snapc;
1333 ceph::real_time mtime;
1334
1335 bufferlist *outbl;
1336 vector<bufferlist*> out_bl;
1337 vector<Context*> out_handler;
1338 vector<int*> out_rval;
1339
1340 int priority;
1341 Context *onfinish;
1342 uint64_t ontimeout;
1343
1344 ceph_tid_t tid;
1345 int attempts;
1346
1347 version_t *objver;
1348 epoch_t *reply_epoch;
1349
1350 ceph::mono_time stamp;
1351
1352 epoch_t map_dne_bound;
1353
1354 bool budgeted;
1355
1356 /// true if we should resend this message on failure
1357 bool should_resend;
1358
1359 /// true if the throttle budget is get/put on a series of OPs,
1360 /// instead of per OP basis, when this flag is set, the budget is
1361 /// acquired before sending the very first OP of the series and
1362 /// released upon receiving the last OP reply.
1363 bool ctx_budgeted;
1364
1365 int *data_offset;
1366
1367 osd_reqid_t reqid; // explicitly setting reqid
1368 ZTracer::Trace trace;
1369
1370 Op(const object_t& o, const object_locator_t& ol, vector<OSDOp>& op,
1371 int f, Context *fin, version_t *ov, int *offset = NULL,
1372 ZTracer::Trace *parent_trace = nullptr) :
1373 session(NULL), incarnation(0),
1374 target(o, ol, f),
1375 con(NULL),
1376 features(CEPH_FEATURES_SUPPORTED_DEFAULT),
1377 snapid(CEPH_NOSNAP),
1378 outbl(NULL),
1379 priority(0),
1380 onfinish(fin),
1381 ontimeout(0),
1382 tid(0),
1383 attempts(0),
1384 objver(ov),
1385 reply_epoch(NULL),
1386 map_dne_bound(0),
1387 budgeted(false),
1388 should_resend(true),
1389 ctx_budgeted(false),
1390 data_offset(offset) {
1391 ops.swap(op);
1392
1393 /* initialize out_* to match op vector */
1394 out_bl.resize(ops.size());
1395 out_rval.resize(ops.size());
1396 out_handler.resize(ops.size());
1397 for (unsigned i = 0; i < ops.size(); i++) {
1398 out_bl[i] = NULL;
1399 out_handler[i] = NULL;
1400 out_rval[i] = NULL;
1401 }
1402
1403 if (target.base_oloc.key == o)
1404 target.base_oloc.key.clear();
1405
31f18b77 1406 if (parent_trace && parent_trace->valid()) {
7c673cae 1407 trace.init("op", nullptr, parent_trace);
31f18b77
FG
1408 trace.event("start");
1409 }
7c673cae
FG
1410 }
1411
1412 bool operator<(const Op& other) const {
1413 return tid < other.tid;
1414 }
1415
1416 bool respects_full() const {
1417 return
1418 (target.flags & (CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_RWORDERED)) &&
1419 !(target.flags & (CEPH_OSD_FLAG_FULL_TRY | CEPH_OSD_FLAG_FULL_FORCE));
1420 }
1421
1422 private:
1423 ~Op() override {
1424 while (!out_handler.empty()) {
1425 delete out_handler.back();
1426 out_handler.pop_back();
1427 }
31f18b77 1428 trace.event("finish");
7c673cae
FG
1429 }
1430 };
1431
1432 struct C_Op_Map_Latest : public Context {
1433 Objecter *objecter;
1434 ceph_tid_t tid;
1435 version_t latest;
1436 C_Op_Map_Latest(Objecter *o, ceph_tid_t t) : objecter(o), tid(t),
1437 latest(0) {}
1438 void finish(int r) override;
1439 };
1440
1441 struct C_Command_Map_Latest : public Context {
1442 Objecter *objecter;
1443 uint64_t tid;
1444 version_t latest;
1445 C_Command_Map_Latest(Objecter *o, ceph_tid_t t) : objecter(o), tid(t),
1446 latest(0) {}
1447 void finish(int r) override;
1448 };
1449
1450 struct C_Stat : public Context {
1451 bufferlist bl;
1452 uint64_t *psize;
1453 ceph::real_time *pmtime;
1454 Context *fin;
1455 C_Stat(uint64_t *ps, ceph::real_time *pm, Context *c) :
1456 psize(ps), pmtime(pm), fin(c) {}
1457 void finish(int r) override {
1458 if (r >= 0) {
1459 bufferlist::iterator p = bl.begin();
1460 uint64_t s;
1461 ceph::real_time m;
1462 ::decode(s, p);
1463 ::decode(m, p);
1464 if (psize)
1465 *psize = s;
1466 if (pmtime)
1467 *pmtime = m;
1468 }
1469 fin->complete(r);
1470 }
1471 };
1472
1473 struct C_GetAttrs : public Context {
1474 bufferlist bl;
1475 map<string,bufferlist>& attrset;
1476 Context *fin;
1477 C_GetAttrs(map<string, bufferlist>& set, Context *c) : attrset(set),
1478 fin(c) {}
1479 void finish(int r) override {
1480 if (r >= 0) {
1481 bufferlist::iterator p = bl.begin();
1482 ::decode(attrset, p);
1483 }
1484 fin->complete(r);
1485 }
1486 };
1487
1488
1489 // Pools and statistics
1490 struct NListContext {
1491 collection_list_handle_t pos;
1492
1493 // these are for !sortbitwise compat only
1494 int current_pg = 0;
1495 int starting_pg_num = 0;
1496 bool sort_bitwise = false;
1497
1498 bool at_end_of_pool = false; ///< publicly visible end flag
1499
1500 int64_t pool_id = -1;
1501 int pool_snap_seq = 0;
1502 uint64_t max_entries = 0;
1503 string nspace;
1504
1505 bufferlist bl; // raw data read to here
1506 std::list<librados::ListObjectImpl> list;
1507
1508 bufferlist filter;
1509
1510 bufferlist extra_info;
1511
1512 // The budget associated with this context, once it is set (>= 0),
1513 // the budget is not get/released on OP basis, instead the budget
1514 // is acquired before sending the first OP and released upon receiving
1515 // the last op reply.
1516 int ctx_budget = -1;
1517
1518 bool at_end() const {
1519 return at_end_of_pool;
1520 }
1521
1522 uint32_t get_pg_hash_position() const {
1523 return pos.get_hash();
1524 }
1525 };
1526
1527 struct C_NList : public Context {
1528 NListContext *list_context;
1529 Context *final_finish;
1530 Objecter *objecter;
1531 epoch_t epoch;
1532 C_NList(NListContext *lc, Context * finish, Objecter *ob) :
1533 list_context(lc), final_finish(finish), objecter(ob), epoch(0) {}
1534 void finish(int r) override {
1535 if (r >= 0) {
1536 objecter->_nlist_reply(list_context, r, final_finish, epoch);
1537 } else {
1538 final_finish->complete(r);
1539 }
1540 }
1541 };
1542
1543 struct PoolStatOp {
1544 ceph_tid_t tid;
1545 list<string> pools;
1546
1547 map<string,pool_stat_t> *pool_stats;
1548 Context *onfinish;
1549 uint64_t ontimeout;
1550
1551 ceph::mono_time last_submit;
1552 };
1553
1554 struct StatfsOp {
1555 ceph_tid_t tid;
1556 struct ceph_statfs *stats;
1557 Context *onfinish;
1558 uint64_t ontimeout;
1559
1560 ceph::mono_time last_submit;
1561 };
1562
1563 struct PoolOp {
1564 ceph_tid_t tid;
1565 int64_t pool;
1566 string name;
1567 Context *onfinish;
1568 uint64_t ontimeout;
1569 int pool_op;
1570 uint64_t auid;
1571 int16_t crush_rule;
1572 snapid_t snapid;
1573 bufferlist *blp;
1574
1575 ceph::mono_time last_submit;
1576 PoolOp() : tid(0), pool(0), onfinish(NULL), ontimeout(0), pool_op(0),
1577 auid(0), crush_rule(0), snapid(0), blp(NULL) {}
1578 };
1579
1580 // -- osd commands --
1581 struct CommandOp : public RefCountedObject {
1582 OSDSession *session = nullptr;
1583 ceph_tid_t tid = 0;
1584 vector<string> cmd;
1585 bufferlist inbl;
1586 bufferlist *poutbl = nullptr;
1587 string *prs = nullptr;
1588
1589 // target_osd == -1 means target_pg is valid
1590 const int target_osd = -1;
1591 const pg_t target_pg;
1592
1593 op_target_t target;
1594
1595 epoch_t map_dne_bound = 0;
1596 int map_check_error = 0; // error to return if map check fails
1597 const char *map_check_error_str = nullptr;
1598
1599 Context *onfinish = nullptr;
1600 uint64_t ontimeout = 0;
1601 ceph::mono_time last_submit;
1602
1603 CommandOp(
1604 int target_osd,
1605 const vector<string> &cmd,
1606 bufferlist inbl,
1607 bufferlist *poutbl,
1608 string *prs,
1609 Context *onfinish)
1610 : cmd(cmd),
1611 inbl(inbl),
1612 poutbl(poutbl),
1613 prs(prs),
1614 target_osd(target_osd),
1615 onfinish(onfinish) {}
1616
1617 CommandOp(
1618 pg_t pgid,
1619 const vector<string> &cmd,
1620 bufferlist inbl,
1621 bufferlist *poutbl,
1622 string *prs,
1623 Context *onfinish)
1624 : cmd(cmd),
1625 inbl(inbl),
1626 poutbl(poutbl),
1627 prs(prs),
1628 target_pg(pgid),
1629 target(pgid),
1630 onfinish(onfinish) {}
1631
1632 };
1633
1634 void submit_command(CommandOp *c, ceph_tid_t *ptid);
1635 int _calc_command_target(CommandOp *c, shunique_lock &sul);
1636 void _assign_command_session(CommandOp *c, shunique_lock &sul);
1637 void _send_command(CommandOp *c);
1638 int command_op_cancel(OSDSession *s, ceph_tid_t tid, int r);
1639 void _finish_command(CommandOp *c, int r, string rs);
1640 void handle_command_reply(MCommandReply *m);
1641
1642
1643 // -- lingering ops --
1644
1645 struct WatchContext {
1646 // this simply mirrors librados WatchCtx2
1647 virtual void handle_notify(uint64_t notify_id,
1648 uint64_t cookie,
1649 uint64_t notifier_id,
1650 bufferlist& bl) = 0;
1651 virtual void handle_error(uint64_t cookie, int err) = 0;
1652 virtual ~WatchContext() {}
1653 };
1654
1655 struct LingerOp : public RefCountedObject {
1656 uint64_t linger_id;
1657
1658 op_target_t target;
1659
1660 snapid_t snap;
1661 SnapContext snapc;
1662 ceph::real_time mtime;
1663
1664 vector<OSDOp> ops;
1665 bufferlist inbl;
1666 bufferlist *poutbl;
1667 version_t *pobjver;
1668
1669 bool is_watch;
1670 ceph::mono_time watch_valid_thru; ///< send time for last acked ping
1671 int last_error; ///< error from last failed ping|reconnect, if any
1672 boost::shared_mutex watch_lock;
1673 using lock_guard = std::unique_lock<decltype(watch_lock)>;
1674 using unique_lock = std::unique_lock<decltype(watch_lock)>;
1675 using shared_lock = boost::shared_lock<decltype(watch_lock)>;
1676 using shunique_lock = ceph::shunique_lock<decltype(watch_lock)>;
1677
1678 // queue of pending async operations, with the timestamp of
1679 // when they were queued.
1680 list<ceph::mono_time> watch_pending_async;
1681
1682 uint32_t register_gen;
1683 bool registered;
1684 bool canceled;
1685 Context *on_reg_commit;
1686
1687 // we trigger these from an async finisher
1688 Context *on_notify_finish;
1689 bufferlist *notify_result_bl;
1690 uint64_t notify_id;
1691
1692 WatchContext *watch_context;
1693
1694 OSDSession *session;
1695
1696 ceph_tid_t register_tid;
1697 ceph_tid_t ping_tid;
1698 epoch_t map_dne_bound;
1699
1700 void _queued_async() {
1701 // watch_lock ust be locked unique
1702 watch_pending_async.push_back(ceph::mono_clock::now());
1703 }
1704 void finished_async() {
1705 unique_lock l(watch_lock);
1706 assert(!watch_pending_async.empty());
1707 watch_pending_async.pop_front();
1708 }
1709
1710 LingerOp() : linger_id(0),
1711 target(object_t(), object_locator_t(), 0),
1712 snap(CEPH_NOSNAP), poutbl(NULL), pobjver(NULL),
1713 is_watch(false), last_error(0),
1714 register_gen(0),
1715 registered(false),
1716 canceled(false),
1717 on_reg_commit(NULL),
1718 on_notify_finish(NULL),
1719 notify_result_bl(NULL),
1720 notify_id(0),
1721 watch_context(NULL),
1722 session(NULL),
1723 register_tid(0),
1724 ping_tid(0),
1725 map_dne_bound(0) {}
1726
1727 // no copy!
1728 const LingerOp &operator=(const LingerOp& r);
1729 LingerOp(const LingerOp& o);
1730
1731 uint64_t get_cookie() {
1732 return reinterpret_cast<uint64_t>(this);
1733 }
1734
1735 private:
1736 ~LingerOp() override {
1737 delete watch_context;
1738 }
1739 };
1740
1741 struct C_Linger_Commit : public Context {
1742 Objecter *objecter;
1743 LingerOp *info;
1744 bufferlist outbl; // used for notify only
1745 C_Linger_Commit(Objecter *o, LingerOp *l) : objecter(o), info(l) {
1746 info->get();
1747 }
1748 ~C_Linger_Commit() override {
1749 info->put();
1750 }
1751 void finish(int r) override {
1752 objecter->_linger_commit(info, r, outbl);
1753 }
1754 };
1755
1756 struct C_Linger_Reconnect : public Context {
1757 Objecter *objecter;
1758 LingerOp *info;
1759 C_Linger_Reconnect(Objecter *o, LingerOp *l) : objecter(o), info(l) {
1760 info->get();
1761 }
1762 ~C_Linger_Reconnect() override {
1763 info->put();
1764 }
1765 void finish(int r) override {
1766 objecter->_linger_reconnect(info, r);
1767 }
1768 };
1769
1770 struct C_Linger_Ping : public Context {
1771 Objecter *objecter;
1772 LingerOp *info;
1773 ceph::mono_time sent;
1774 uint32_t register_gen;
1775 C_Linger_Ping(Objecter *o, LingerOp *l)
1776 : objecter(o), info(l), register_gen(info->register_gen) {
1777 info->get();
1778 }
1779 ~C_Linger_Ping() override {
1780 info->put();
1781 }
1782 void finish(int r) override {
1783 objecter->_linger_ping(info, r, sent, register_gen);
1784 }
1785 };
1786
1787 struct C_Linger_Map_Latest : public Context {
1788 Objecter *objecter;
1789 uint64_t linger_id;
1790 version_t latest;
1791 C_Linger_Map_Latest(Objecter *o, uint64_t id) :
1792 objecter(o), linger_id(id), latest(0) {}
1793 void finish(int r) override;
1794 };
1795
1796 // -- osd sessions --
1797 struct OSDBackoff {
1798 spg_t pgid;
1799 uint64_t id;
1800 hobject_t begin, end;
1801 };
1802
1803 struct OSDSession : public RefCountedObject {
1804 boost::shared_mutex lock;
1805 using lock_guard = std::lock_guard<decltype(lock)>;
1806 using unique_lock = std::unique_lock<decltype(lock)>;
1807 using shared_lock = boost::shared_lock<decltype(lock)>;
1808 using shunique_lock = ceph::shunique_lock<decltype(lock)>;
1809
1810 // pending ops
1811 map<ceph_tid_t,Op*> ops;
1812 map<uint64_t, LingerOp*> linger_ops;
1813 map<ceph_tid_t,CommandOp*> command_ops;
1814
1815 // backoffs
1816 map<spg_t,map<hobject_t,OSDBackoff>> backoffs;
1817 map<uint64_t,OSDBackoff*> backoffs_by_id;
1818
1819 int osd;
1820 int incarnation;
1821 ConnectionRef con;
1822 int num_locks;
1823 std::unique_ptr<std::mutex[]> completion_locks;
1824 using unique_completion_lock = std::unique_lock<
1825 decltype(completion_locks)::element_type>;
1826
1827
1828 OSDSession(CephContext *cct, int o) :
1829 osd(o), incarnation(0), con(NULL),
1830 num_locks(cct->_conf->objecter_completion_locks_per_session),
1831 completion_locks(new std::mutex[num_locks]) {}
1832
1833 ~OSDSession() override;
1834
1835 bool is_homeless() { return (osd == -1); }
1836
1837 unique_completion_lock get_lock(object_t& oid);
1838 };
1839 map<int,OSDSession*> osd_sessions;
1840
1841 bool osdmap_full_flag() const;
1842 bool osdmap_pool_full(const int64_t pool_id) const;
1843
1844 private:
1845
1846 /**
1847 * Test pg_pool_t::FLAG_FULL on a pool
1848 *
1849 * @return true if the pool exists and has the flag set, or
1850 * the global full flag is set, else false
1851 */
1852 bool _osdmap_pool_full(const int64_t pool_id) const;
1853 bool _osdmap_pool_full(const pg_pool_t &p) const;
1854 void update_pool_full_map(map<int64_t, bool>& pool_full_map);
1855
1856 map<uint64_t, LingerOp*> linger_ops;
1857 // we use this just to confirm a cookie is valid before dereferencing the ptr
1858 set<LingerOp*> linger_ops_set;
1859
1860 map<ceph_tid_t,PoolStatOp*> poolstat_ops;
1861 map<ceph_tid_t,StatfsOp*> statfs_ops;
1862 map<ceph_tid_t,PoolOp*> pool_ops;
31f18b77 1863 std::atomic<unsigned> num_homeless_ops{0};
7c673cae
FG
1864
1865 OSDSession *homeless_session;
1866
1867 // ops waiting for an osdmap with a new pool or confirmation that
1868 // the pool does not exist (may be expanded to other uses later)
1869 map<uint64_t, LingerOp*> check_latest_map_lingers;
1870 map<ceph_tid_t, Op*> check_latest_map_ops;
1871 map<ceph_tid_t, CommandOp*> check_latest_map_commands;
1872
1873 map<epoch_t,list< pair<Context*, int> > > waiting_for_map;
1874
1875 ceph::timespan mon_timeout;
1876 ceph::timespan osd_timeout;
1877
1878 MOSDOp *_prepare_osd_op(Op *op);
1879 void _send_op(Op *op, MOSDOp *m = NULL);
1880 void _send_op_account(Op *op);
1881 void _cancel_linger_op(Op *op);
1882 void finish_op(OSDSession *session, ceph_tid_t tid);
1883 void _finish_op(Op *op, int r);
1884 static bool is_pg_changed(
1885 int oldprimary,
1886 const vector<int>& oldacting,
1887 int newprimary,
1888 const vector<int>& newacting,
1889 bool any_change=false);
1890 enum recalc_op_target_result {
1891 RECALC_OP_TARGET_NO_ACTION = 0,
1892 RECALC_OP_TARGET_NEED_RESEND,
1893 RECALC_OP_TARGET_POOL_DNE,
1894 RECALC_OP_TARGET_OSD_DNE,
1895 RECALC_OP_TARGET_OSD_DOWN,
1896 };
1897 bool _osdmap_full_flag() const;
1898 bool _osdmap_has_pool_full() const;
1899
1900 bool target_should_be_paused(op_target_t *op);
1901 int _calc_target(op_target_t *t, Connection *con,
1902 bool any_change = false);
1903 int _map_session(op_target_t *op, OSDSession **s,
1904 shunique_lock& lc);
1905
1906 void _session_op_assign(OSDSession *s, Op *op);
1907 void _session_op_remove(OSDSession *s, Op *op);
1908 void _session_linger_op_assign(OSDSession *to, LingerOp *op);
1909 void _session_linger_op_remove(OSDSession *from, LingerOp *op);
1910 void _session_command_op_assign(OSDSession *to, CommandOp *op);
1911 void _session_command_op_remove(OSDSession *from, CommandOp *op);
1912
1913 int _assign_op_target_session(Op *op, shunique_lock& lc,
1914 bool src_session_locked,
1915 bool dst_session_locked);
1916 int _recalc_linger_op_target(LingerOp *op, shunique_lock& lc);
1917
1918 void _linger_submit(LingerOp *info, shunique_lock& sul);
1919 void _send_linger(LingerOp *info, shunique_lock& sul);
1920 void _linger_commit(LingerOp *info, int r, bufferlist& outbl);
1921 void _linger_reconnect(LingerOp *info, int r);
1922 void _send_linger_ping(LingerOp *info);
1923 void _linger_ping(LingerOp *info, int r, ceph::mono_time sent,
1924 uint32_t register_gen);
1925 int _normalize_watch_error(int r);
1926
1927 friend class C_DoWatchError;
1928public:
1929 void linger_callback_flush(Context *ctx) {
1930 finisher->queue(ctx);
1931 }
1932
1933private:
1934 void _check_op_pool_dne(Op *op, unique_lock *sl);
1935 void _send_op_map_check(Op *op);
1936 void _op_cancel_map_check(Op *op);
1937 void _check_linger_pool_dne(LingerOp *op, bool *need_unregister);
1938 void _send_linger_map_check(LingerOp *op);
1939 void _linger_cancel_map_check(LingerOp *op);
1940 void _check_command_map_dne(CommandOp *op);
1941 void _send_command_map_check(CommandOp *op);
1942 void _command_cancel_map_check(CommandOp *op);
1943
1944 void kick_requests(OSDSession *session);
1945 void _kick_requests(OSDSession *session, map<uint64_t, LingerOp *>& lresend);
1946 void _linger_ops_resend(map<uint64_t, LingerOp *>& lresend, unique_lock& ul);
1947
1948 int _get_session(int osd, OSDSession **session, shunique_lock& sul);
1949 void put_session(OSDSession *s);
1950 void get_session(OSDSession *s);
1951 void _reopen_session(OSDSession *session);
1952 void close_session(OSDSession *session);
1953
1954 void _nlist_reply(NListContext *list_context, int r, Context *final_finish,
1955 epoch_t reply_epoch);
1956
1957 void resend_mon_ops();
1958
1959 /**
1960 * handle a budget for in-flight ops
1961 * budget is taken whenever an op goes into the ops map
1962 * and returned whenever an op is removed from the map
1963 * If throttle_op needs to throttle it will unlock client_lock.
1964 */
1965 int calc_op_budget(Op *op);
1966 void _throttle_op(Op *op, shunique_lock& sul, int op_size = 0);
1967 int _take_op_budget(Op *op, shunique_lock& sul) {
1968 assert(sul && sul.mutex() == &rwlock);
1969 int op_budget = calc_op_budget(op);
1970 if (keep_balanced_budget) {
1971 _throttle_op(op, sul, op_budget);
1972 } else {
1973 op_throttle_bytes.take(op_budget);
1974 op_throttle_ops.take(1);
1975 }
1976 op->budgeted = true;
1977 return op_budget;
1978 }
1979 void put_op_budget_bytes(int op_budget) {
1980 assert(op_budget >= 0);
1981 op_throttle_bytes.put(op_budget);
1982 op_throttle_ops.put(1);
1983 }
1984 void put_op_budget(Op *op) {
1985 assert(op->budgeted);
1986 int op_budget = calc_op_budget(op);
1987 put_op_budget_bytes(op_budget);
1988 }
1989 void put_nlist_context_budget(NListContext *list_context);
1990 Throttle op_throttle_bytes, op_throttle_ops;
1991
1992 public:
1993 Objecter(CephContext *cct_, Messenger *m, MonClient *mc,
1994 Finisher *fin,
1995 double mon_timeout,
1996 double osd_timeout) :
1997 Dispatcher(cct_), messenger(m), monc(mc), finisher(fin),
1998 trace_endpoint("0.0.0.0", 0, "Objecter"),
31f18b77
FG
1999 osdmap(new OSDMap),
2000 max_linger_id(0),
7c673cae 2001 keep_balanced_budget(false), honor_osdmap_full(true), osdmap_full_try(false),
31f18b77 2002 blacklist_events_enabled(false),
7c673cae
FG
2003 last_seen_osdmap_version(0), last_seen_pgmap_version(0),
2004 logger(NULL), tick_event(0), m_request_state_hook(NULL),
7c673cae
FG
2005 homeless_session(new OSDSession(cct, -1)),
2006 mon_timeout(ceph::make_timespan(mon_timeout)),
2007 osd_timeout(ceph::make_timespan(osd_timeout)),
2008 op_throttle_bytes(cct, "objecter_bytes",
2009 cct->_conf->objecter_inflight_op_bytes),
2010 op_throttle_ops(cct, "objecter_ops", cct->_conf->objecter_inflight_ops),
2011 epoch_barrier(0),
2012 retry_writes_after_first_reply(cct->_conf->objecter_retry_writes_after_first_reply)
2013 { }
2014 ~Objecter() override;
2015
2016 void init();
2017 void start(const OSDMap *o = nullptr);
2018 void shutdown();
2019
2020 // These two templates replace osdmap_(get)|(put)_read. Simply wrap
2021 // whatever functionality you want to use the OSDMap in a lambda like:
2022 //
2023 // with_osdmap([](const OSDMap& o) { o.do_stuff(); });
2024 //
2025 // or
2026 //
2027 // auto t = with_osdmap([&](const OSDMap& o) { return o.lookup_stuff(x); });
2028 //
2029 // Do not call into something that will try to lock the OSDMap from
2030 // here or you will have great woe and misery.
2031
2032 template<typename Callback, typename...Args>
2033 auto with_osdmap(Callback&& cb, Args&&... args) const ->
2034 decltype(cb(*osdmap, std::forward<Args>(args)...)) {
2035 shared_lock l(rwlock);
2036 return std::forward<Callback>(cb)(*osdmap, std::forward<Args>(args)...);
2037 }
2038
2039
2040 /**
2041 * Tell the objecter to throttle outgoing ops according to its
2042 * budget (in _conf). If you do this, ops can block, in
2043 * which case it will unlock client_lock and sleep until
2044 * incoming messages reduce the used budget low enough for
2045 * the ops to continue going; then it will lock client_lock again.
2046 */
2047 void set_balanced_budget() { keep_balanced_budget = true; }
2048 void unset_balanced_budget() { keep_balanced_budget = false; }
2049
2050 void set_honor_osdmap_full() { honor_osdmap_full = true; }
2051 void unset_honor_osdmap_full() { honor_osdmap_full = false; }
2052
2053 void set_osdmap_full_try() { osdmap_full_try = true; }
2054 void unset_osdmap_full_try() { osdmap_full_try = false; }
2055
2056 void _scan_requests(OSDSession *s,
2057 bool force_resend,
2058 bool cluster_full,
2059 map<int64_t, bool> *pool_full_map,
2060 map<ceph_tid_t, Op*>& need_resend,
2061 list<LingerOp*>& need_resend_linger,
2062 map<ceph_tid_t, CommandOp*>& need_resend_command,
2063 shunique_lock& sul);
2064
2065 int64_t get_object_hash_position(int64_t pool, const string& key,
2066 const string& ns);
2067 int64_t get_object_pg_hash_position(int64_t pool, const string& key,
2068 const string& ns);
2069
2070 // messages
2071 public:
2072 bool ms_dispatch(Message *m) override;
2073 bool ms_can_fast_dispatch_any() const override {
2074 return true;
2075 }
2076 bool ms_can_fast_dispatch(const Message *m) const override {
2077 switch (m->get_type()) {
2078 case CEPH_MSG_OSD_OPREPLY:
2079 case CEPH_MSG_WATCH_NOTIFY:
2080 return true;
2081 default:
2082 return false;
2083 }
2084 }
2085 void ms_fast_dispatch(Message *m) override {
224ce89b
WB
2086 if (!ms_dispatch(m)) {
2087 m->put();
2088 }
7c673cae
FG
2089 }
2090
2091 void handle_osd_op_reply(class MOSDOpReply *m);
2092 void handle_osd_backoff(class MOSDBackoff *m);
2093 void handle_watch_notify(class MWatchNotify *m);
2094 void handle_osd_map(class MOSDMap *m);
2095 void wait_for_osd_map();
2096
31f18b77
FG
2097 /**
2098 * Get list of entities blacklisted since this was last called,
2099 * and reset the list.
2100 *
2101 * Uses a std::set because typical use case is to compare some
2102 * other list of clients to see which overlap with the blacklisted
2103 * addrs.
2104 *
2105 */
2106 void consume_blacklist_events(std::set<entity_addr_t> *events);
2107
7c673cae
FG
2108 int pool_snap_by_name(int64_t poolid,
2109 const char *snap_name,
2110 snapid_t *snap) const;
2111 int pool_snap_get_info(int64_t poolid, snapid_t snap,
2112 pool_snap_info_t *info) const;
2113 int pool_snap_list(int64_t poolid, vector<uint64_t> *snaps);
2114private:
2115
31f18b77
FG
2116 void emit_blacklist_events(const OSDMap::Incremental &inc);
2117 void emit_blacklist_events(const OSDMap &old_osd_map,
2118 const OSDMap &new_osd_map);
2119
7c673cae
FG
2120 // low-level
2121 void _op_submit(Op *op, shunique_lock& lc, ceph_tid_t *ptid);
2122 void _op_submit_with_budget(Op *op, shunique_lock& lc,
2123 ceph_tid_t *ptid,
2124 int *ctx_budget = NULL);
2125 inline void unregister_op(Op *op);
2126
2127 // public interface
2128public:
2129 void op_submit(Op *op, ceph_tid_t *ptid = NULL, int *ctx_budget = NULL);
2130 bool is_active() {
2131 shared_lock l(rwlock);
31f18b77 2132 return !((!inflight_ops) && linger_ops.empty() &&
7c673cae
FG
2133 poolstat_ops.empty() && statfs_ops.empty());
2134 }
2135
2136 /**
2137 * Output in-flight requests
2138 */
2139 void _dump_active(OSDSession *s);
2140 void _dump_active();
2141 void dump_active();
2142 void dump_requests(Formatter *fmt);
2143 void _dump_ops(const OSDSession *s, Formatter *fmt);
2144 void dump_ops(Formatter *fmt);
2145 void _dump_linger_ops(const OSDSession *s, Formatter *fmt);
2146 void dump_linger_ops(Formatter *fmt);
2147 void _dump_command_ops(const OSDSession *s, Formatter *fmt);
2148 void dump_command_ops(Formatter *fmt);
2149 void dump_pool_ops(Formatter *fmt) const;
2150 void dump_pool_stat_ops(Formatter *fmt) const;
2151 void dump_statfs_ops(Formatter *fmt) const;
2152
31f18b77
FG
2153 int get_client_incarnation() const { return client_inc; }
2154 void set_client_incarnation(int inc) { client_inc = inc; }
7c673cae
FG
2155
2156 bool have_map(epoch_t epoch);
2157 /// wait for epoch; true if we already have it
2158 bool wait_for_map(epoch_t epoch, Context *c, int err=0);
2159 void _wait_for_new_map(Context *c, epoch_t epoch, int err=0);
2160 void wait_for_latest_osdmap(Context *fin);
2161 void get_latest_version(epoch_t oldest, epoch_t neweset, Context *fin);
2162 void _get_latest_version(epoch_t oldest, epoch_t neweset, Context *fin);
2163
2164 /** Get the current set of global op flags */
31f18b77 2165 int get_global_op_flags() const { return global_op_flags; }
7c673cae
FG
2166 /** Add a flag to the global op flags, not really atomic operation */
2167 void add_global_op_flags(int flag) {
31f18b77 2168 global_op_flags.fetch_or(flag);
7c673cae 2169 }
31f18b77 2170 /** Clear the passed flags from the global op flag set */
7c673cae 2171 void clear_global_op_flag(int flags) {
31f18b77 2172 global_op_flags.fetch_and(~flags);
7c673cae
FG
2173 }
2174
2175 /// cancel an in-progress request with the given return code
2176private:
2177 int op_cancel(OSDSession *s, ceph_tid_t tid, int r);
2178 int _op_cancel(ceph_tid_t tid, int r);
2179public:
2180 int op_cancel(ceph_tid_t tid, int r);
2181
2182 /**
2183 * Any write op which is in progress at the start of this call shall no
2184 * longer be in progress when this call ends. Operations started after the
2185 * start of this call may still be in progress when this call ends.
2186 *
2187 * @return the latest possible epoch in which a cancelled op could have
2188 * existed, or -1 if nothing was cancelled.
2189 */
2190 epoch_t op_cancel_writes(int r, int64_t pool=-1);
2191
2192 // commands
2193 void osd_command(int osd, const std::vector<string>& cmd,
2194 const bufferlist& inbl, ceph_tid_t *ptid,
2195 bufferlist *poutbl, string *prs, Context *onfinish) {
2196 assert(osd >= 0);
2197 CommandOp *c = new CommandOp(
2198 osd,
2199 cmd,
2200 inbl,
2201 poutbl,
2202 prs,
2203 onfinish);
2204 submit_command(c, ptid);
2205 }
224ce89b 2206 void pg_command(pg_t pgid, const vector<string>& cmd,
7c673cae
FG
2207 const bufferlist& inbl, ceph_tid_t *ptid,
2208 bufferlist *poutbl, string *prs, Context *onfinish) {
2209 CommandOp *c = new CommandOp(
2210 pgid,
2211 cmd,
2212 inbl,
2213 poutbl,
2214 prs,
2215 onfinish);
2216 submit_command(c, ptid);
2217 }
2218
2219 // mid-level helpers
2220 Op *prepare_mutate_op(
2221 const object_t& oid, const object_locator_t& oloc,
2222 ObjectOperation& op, const SnapContext& snapc,
2223 ceph::real_time mtime, int flags,
2224 Context *oncommit, version_t *objver = NULL,
2225 osd_reqid_t reqid = osd_reqid_t(),
2226 ZTracer::Trace *parent_trace = nullptr) {
31f18b77 2227 Op *o = new Op(oid, oloc, op.ops, flags | global_op_flags |
7c673cae
FG
2228 CEPH_OSD_FLAG_WRITE, oncommit, objver, nullptr, parent_trace);
2229 o->priority = op.priority;
2230 o->mtime = mtime;
2231 o->snapc = snapc;
2232 o->out_rval.swap(op.out_rval);
2233 o->reqid = reqid;
2234 return o;
2235 }
2236 ceph_tid_t mutate(
2237 const object_t& oid, const object_locator_t& oloc,
2238 ObjectOperation& op, const SnapContext& snapc,
2239 ceph::real_time mtime, int flags,
2240 Context *oncommit, version_t *objver = NULL,
2241 osd_reqid_t reqid = osd_reqid_t()) {
2242 Op *o = prepare_mutate_op(oid, oloc, op, snapc, mtime, flags,
2243 oncommit, objver, reqid);
2244 ceph_tid_t tid;
2245 op_submit(o, &tid);
2246 return tid;
2247 }
2248 Op *prepare_read_op(
2249 const object_t& oid, const object_locator_t& oloc,
2250 ObjectOperation& op,
2251 snapid_t snapid, bufferlist *pbl, int flags,
2252 Context *onack, version_t *objver = NULL,
2253 int *data_offset = NULL,
2254 uint64_t features = 0,
2255 ZTracer::Trace *parent_trace = nullptr) {
31f18b77 2256 Op *o = new Op(oid, oloc, op.ops, flags | global_op_flags |
7c673cae
FG
2257 CEPH_OSD_FLAG_READ, onack, objver, data_offset, parent_trace);
2258 o->priority = op.priority;
2259 o->snapid = snapid;
2260 o->outbl = pbl;
2261 if (!o->outbl && op.size() == 1 && op.out_bl[0]->length())
2262 o->outbl = op.out_bl[0];
2263 o->out_bl.swap(op.out_bl);
2264 o->out_handler.swap(op.out_handler);
2265 o->out_rval.swap(op.out_rval);
2266 return o;
2267 }
2268 ceph_tid_t read(
2269 const object_t& oid, const object_locator_t& oloc,
2270 ObjectOperation& op,
2271 snapid_t snapid, bufferlist *pbl, int flags,
2272 Context *onack, version_t *objver = NULL,
2273 int *data_offset = NULL,
2274 uint64_t features = 0) {
2275 Op *o = prepare_read_op(oid, oloc, op, snapid, pbl, flags, onack, objver,
2276 data_offset);
2277 if (features)
2278 o->features = features;
2279 ceph_tid_t tid;
2280 op_submit(o, &tid);
2281 return tid;
2282 }
2283 Op *prepare_pg_read_op(
2284 uint32_t hash, object_locator_t oloc,
2285 ObjectOperation& op, bufferlist *pbl, int flags,
2286 Context *onack, epoch_t *reply_epoch,
2287 int *ctx_budget) {
2288 Op *o = new Op(object_t(), oloc,
2289 op.ops,
31f18b77 2290 flags | global_op_flags | CEPH_OSD_FLAG_READ |
7c673cae
FG
2291 CEPH_OSD_FLAG_IGNORE_OVERLAY,
2292 onack, NULL);
2293 o->target.precalc_pgid = true;
2294 o->target.base_pgid = pg_t(hash, oloc.pool);
2295 o->priority = op.priority;
2296 o->snapid = CEPH_NOSNAP;
2297 o->outbl = pbl;
2298 o->out_bl.swap(op.out_bl);
2299 o->out_handler.swap(op.out_handler);
2300 o->out_rval.swap(op.out_rval);
2301 o->reply_epoch = reply_epoch;
2302 if (ctx_budget) {
2303 // budget is tracked by listing context
2304 o->ctx_budgeted = true;
2305 }
2306 return o;
2307 }
2308 ceph_tid_t pg_read(
2309 uint32_t hash, object_locator_t oloc,
2310 ObjectOperation& op, bufferlist *pbl, int flags,
2311 Context *onack, epoch_t *reply_epoch,
2312 int *ctx_budget) {
2313 Op *o = prepare_pg_read_op(hash, oloc, op, pbl, flags,
2314 onack, reply_epoch, ctx_budget);
2315 ceph_tid_t tid;
2316 op_submit(o, &tid, ctx_budget);
2317 return tid;
2318 }
2319
2320 // caller owns a ref
2321 LingerOp *linger_register(const object_t& oid, const object_locator_t& oloc,
2322 int flags);
2323 ceph_tid_t linger_watch(LingerOp *info,
2324 ObjectOperation& op,
2325 const SnapContext& snapc, ceph::real_time mtime,
2326 bufferlist& inbl,
2327 Context *onfinish,
2328 version_t *objver);
2329 ceph_tid_t linger_notify(LingerOp *info,
2330 ObjectOperation& op,
2331 snapid_t snap, bufferlist& inbl,
2332 bufferlist *poutbl,
2333 Context *onack,
2334 version_t *objver);
2335 int linger_check(LingerOp *info);
2336 void linger_cancel(LingerOp *info); // releases a reference
2337 void _linger_cancel(LingerOp *info);
2338
2339 void _do_watch_notify(LingerOp *info, MWatchNotify *m);
2340
2341 /**
2342 * set up initial ops in the op vector, and allocate a final op slot.
2343 *
2344 * The caller is responsible for filling in the final ops_count ops.
2345 *
2346 * @param ops op vector
2347 * @param ops_count number of final ops the caller will fill in
2348 * @param extra_ops pointer to [array of] initial op[s]
2349 * @return index of final op (for caller to fill in)
2350 */
2351 int init_ops(vector<OSDOp>& ops, int ops_count, ObjectOperation *extra_ops) {
2352 int i;
2353 int extra = 0;
2354
2355 if (extra_ops)
2356 extra = extra_ops->ops.size();
2357
2358 ops.resize(ops_count + extra);
2359
2360 for (i=0; i<extra; i++) {
2361 ops[i] = extra_ops->ops[i];
2362 }
2363
2364 return i;
2365 }
2366
2367
2368 // high-level helpers
2369 Op *prepare_stat_op(
2370 const object_t& oid, const object_locator_t& oloc,
2371 snapid_t snap, uint64_t *psize, ceph::real_time *pmtime,
2372 int flags, Context *onfinish, version_t *objver = NULL,
2373 ObjectOperation *extra_ops = NULL) {
2374 vector<OSDOp> ops;
2375 int i = init_ops(ops, 1, extra_ops);
2376 ops[i].op.op = CEPH_OSD_OP_STAT;
2377 C_Stat *fin = new C_Stat(psize, pmtime, onfinish);
31f18b77 2378 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2379 CEPH_OSD_FLAG_READ, fin, objver);
2380 o->snapid = snap;
2381 o->outbl = &fin->bl;
2382 return o;
2383 }
2384 ceph_tid_t stat(
2385 const object_t& oid, const object_locator_t& oloc,
2386 snapid_t snap, uint64_t *psize, ceph::real_time *pmtime,
2387 int flags, Context *onfinish, version_t *objver = NULL,
2388 ObjectOperation *extra_ops = NULL) {
2389 Op *o = prepare_stat_op(oid, oloc, snap, psize, pmtime, flags,
2390 onfinish, objver, extra_ops);
2391 ceph_tid_t tid;
2392 op_submit(o, &tid);
2393 return tid;
2394 }
2395
2396 Op *prepare_read_op(
2397 const object_t& oid, const object_locator_t& oloc,
2398 uint64_t off, uint64_t len, snapid_t snap, bufferlist *pbl,
2399 int flags, Context *onfinish, version_t *objver = NULL,
2400 ObjectOperation *extra_ops = NULL, int op_flags = 0,
2401 ZTracer::Trace *parent_trace = nullptr) {
2402 vector<OSDOp> ops;
2403 int i = init_ops(ops, 1, extra_ops);
2404 ops[i].op.op = CEPH_OSD_OP_READ;
2405 ops[i].op.extent.offset = off;
2406 ops[i].op.extent.length = len;
2407 ops[i].op.extent.truncate_size = 0;
2408 ops[i].op.extent.truncate_seq = 0;
2409 ops[i].op.flags = op_flags;
31f18b77 2410 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2411 CEPH_OSD_FLAG_READ, onfinish, objver, nullptr, parent_trace);
2412 o->snapid = snap;
2413 o->outbl = pbl;
2414 return o;
2415 }
2416 ceph_tid_t read(
2417 const object_t& oid, const object_locator_t& oloc,
2418 uint64_t off, uint64_t len, snapid_t snap, bufferlist *pbl,
2419 int flags, Context *onfinish, version_t *objver = NULL,
2420 ObjectOperation *extra_ops = NULL, int op_flags = 0) {
2421 Op *o = prepare_read_op(oid, oloc, off, len, snap, pbl, flags,
2422 onfinish, objver, extra_ops, op_flags);
2423 ceph_tid_t tid;
2424 op_submit(o, &tid);
2425 return tid;
2426 }
2427
2428 Op *prepare_cmpext_op(
2429 const object_t& oid, const object_locator_t& oloc,
2430 uint64_t off, bufferlist &cmp_bl,
2431 snapid_t snap, int flags, Context *onfinish, version_t *objver = NULL,
2432 ObjectOperation *extra_ops = NULL, int op_flags = 0) {
2433 vector<OSDOp> ops;
2434 int i = init_ops(ops, 1, extra_ops);
2435 ops[i].op.op = CEPH_OSD_OP_CMPEXT;
2436 ops[i].op.extent.offset = off;
2437 ops[i].op.extent.length = cmp_bl.length();
2438 ops[i].op.extent.truncate_size = 0;
2439 ops[i].op.extent.truncate_seq = 0;
2440 ops[i].indata = cmp_bl;
2441 ops[i].op.flags = op_flags;
31f18b77 2442 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2443 CEPH_OSD_FLAG_READ, onfinish, objver);
2444 o->snapid = snap;
2445 return o;
2446 }
2447
2448 ceph_tid_t cmpext(
2449 const object_t& oid, const object_locator_t& oloc,
2450 uint64_t off, bufferlist &cmp_bl,
2451 snapid_t snap, int flags, Context *onfinish, version_t *objver = NULL,
2452 ObjectOperation *extra_ops = NULL, int op_flags = 0) {
2453 Op *o = prepare_cmpext_op(oid, oloc, off, cmp_bl, snap,
2454 flags, onfinish, objver, extra_ops, op_flags);
2455 ceph_tid_t tid;
2456 op_submit(o, &tid);
2457 return tid;
2458 }
2459
2460 ceph_tid_t read_trunc(const object_t& oid, const object_locator_t& oloc,
2461 uint64_t off, uint64_t len, snapid_t snap,
2462 bufferlist *pbl, int flags, uint64_t trunc_size,
2463 __u32 trunc_seq, Context *onfinish,
2464 version_t *objver = NULL,
2465 ObjectOperation *extra_ops = NULL, int op_flags = 0) {
2466 vector<OSDOp> ops;
2467 int i = init_ops(ops, 1, extra_ops);
2468 ops[i].op.op = CEPH_OSD_OP_READ;
2469 ops[i].op.extent.offset = off;
2470 ops[i].op.extent.length = len;
2471 ops[i].op.extent.truncate_size = trunc_size;
2472 ops[i].op.extent.truncate_seq = trunc_seq;
2473 ops[i].op.flags = op_flags;
31f18b77 2474 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2475 CEPH_OSD_FLAG_READ, onfinish, objver);
2476 o->snapid = snap;
2477 o->outbl = pbl;
2478 ceph_tid_t tid;
2479 op_submit(o, &tid);
2480 return tid;
2481 }
2482 ceph_tid_t mapext(const object_t& oid, const object_locator_t& oloc,
2483 uint64_t off, uint64_t len, snapid_t snap, bufferlist *pbl,
2484 int flags, Context *onfinish, version_t *objver = NULL,
2485 ObjectOperation *extra_ops = NULL) {
2486 vector<OSDOp> ops;
2487 int i = init_ops(ops, 1, extra_ops);
2488 ops[i].op.op = CEPH_OSD_OP_MAPEXT;
2489 ops[i].op.extent.offset = off;
2490 ops[i].op.extent.length = len;
2491 ops[i].op.extent.truncate_size = 0;
2492 ops[i].op.extent.truncate_seq = 0;
31f18b77 2493 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2494 CEPH_OSD_FLAG_READ, onfinish, objver);
2495 o->snapid = snap;
2496 o->outbl = pbl;
2497 ceph_tid_t tid;
2498 op_submit(o, &tid);
2499 return tid;
2500 }
2501 ceph_tid_t getxattr(const object_t& oid, const object_locator_t& oloc,
2502 const char *name, snapid_t snap, bufferlist *pbl, int flags,
2503 Context *onfinish,
2504 version_t *objver = NULL, ObjectOperation *extra_ops = NULL) {
2505 vector<OSDOp> ops;
2506 int i = init_ops(ops, 1, extra_ops);
2507 ops[i].op.op = CEPH_OSD_OP_GETXATTR;
2508 ops[i].op.xattr.name_len = (name ? strlen(name) : 0);
2509 ops[i].op.xattr.value_len = 0;
2510 if (name)
2511 ops[i].indata.append(name);
31f18b77 2512 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2513 CEPH_OSD_FLAG_READ, onfinish, objver);
2514 o->snapid = snap;
2515 o->outbl = pbl;
2516 ceph_tid_t tid;
2517 op_submit(o, &tid);
2518 return tid;
2519 }
2520
2521 ceph_tid_t getxattrs(const object_t& oid, const object_locator_t& oloc,
2522 snapid_t snap, map<string,bufferlist>& attrset,
2523 int flags, Context *onfinish, version_t *objver = NULL,
2524 ObjectOperation *extra_ops = NULL) {
2525 vector<OSDOp> ops;
2526 int i = init_ops(ops, 1, extra_ops);
2527 ops[i].op.op = CEPH_OSD_OP_GETXATTRS;
2528 C_GetAttrs *fin = new C_GetAttrs(attrset, onfinish);
31f18b77 2529 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2530 CEPH_OSD_FLAG_READ, fin, objver);
2531 o->snapid = snap;
2532 o->outbl = &fin->bl;
2533 ceph_tid_t tid;
2534 op_submit(o, &tid);
2535 return tid;
2536 }
2537
2538 ceph_tid_t read_full(const object_t& oid, const object_locator_t& oloc,
2539 snapid_t snap, bufferlist *pbl, int flags,
2540 Context *onfinish, version_t *objver = NULL,
2541 ObjectOperation *extra_ops = NULL) {
31f18b77 2542 return read(oid, oloc, 0, 0, snap, pbl, flags | global_op_flags |
7c673cae
FG
2543 CEPH_OSD_FLAG_READ, onfinish, objver, extra_ops);
2544 }
2545
2546
2547 // writes
2548 ceph_tid_t _modify(const object_t& oid, const object_locator_t& oloc,
2549 vector<OSDOp>& ops, ceph::real_time mtime,
2550 const SnapContext& snapc, int flags,
2551 Context *oncommit,
2552 version_t *objver = NULL) {
31f18b77 2553 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2554 CEPH_OSD_FLAG_WRITE, oncommit, objver);
2555 o->mtime = mtime;
2556 o->snapc = snapc;
2557 ceph_tid_t tid;
2558 op_submit(o, &tid);
2559 return tid;
2560 }
2561 Op *prepare_write_op(
2562 const object_t& oid, const object_locator_t& oloc,
2563 uint64_t off, uint64_t len, const SnapContext& snapc,
2564 const bufferlist &bl, ceph::real_time mtime, int flags,
2565 Context *oncommit, version_t *objver = NULL,
2566 ObjectOperation *extra_ops = NULL, int op_flags = 0,
2567 ZTracer::Trace *parent_trace = nullptr) {
2568 vector<OSDOp> ops;
2569 int i = init_ops(ops, 1, extra_ops);
2570 ops[i].op.op = CEPH_OSD_OP_WRITE;
2571 ops[i].op.extent.offset = off;
2572 ops[i].op.extent.length = len;
2573 ops[i].op.extent.truncate_size = 0;
2574 ops[i].op.extent.truncate_seq = 0;
2575 ops[i].indata = bl;
2576 ops[i].op.flags = op_flags;
31f18b77 2577 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2578 CEPH_OSD_FLAG_WRITE, oncommit, objver,
2579 nullptr, parent_trace);
2580 o->mtime = mtime;
2581 o->snapc = snapc;
2582 return o;
2583 }
2584 ceph_tid_t write(
2585 const object_t& oid, const object_locator_t& oloc,
2586 uint64_t off, uint64_t len, const SnapContext& snapc,
2587 const bufferlist &bl, ceph::real_time mtime, int flags,
2588 Context *oncommit, version_t *objver = NULL,
2589 ObjectOperation *extra_ops = NULL, int op_flags = 0) {
2590 Op *o = prepare_write_op(oid, oloc, off, len, snapc, bl, mtime, flags,
2591 oncommit, objver, extra_ops, op_flags);
2592 ceph_tid_t tid;
2593 op_submit(o, &tid);
2594 return tid;
2595 }
2596 Op *prepare_append_op(
2597 const object_t& oid, const object_locator_t& oloc,
2598 uint64_t len, const SnapContext& snapc,
2599 const bufferlist &bl, ceph::real_time mtime, int flags,
2600 Context *oncommit,
2601 version_t *objver = NULL,
2602 ObjectOperation *extra_ops = NULL) {
2603 vector<OSDOp> ops;
2604 int i = init_ops(ops, 1, extra_ops);
2605 ops[i].op.op = CEPH_OSD_OP_APPEND;
2606 ops[i].op.extent.offset = 0;
2607 ops[i].op.extent.length = len;
2608 ops[i].op.extent.truncate_size = 0;
2609 ops[i].op.extent.truncate_seq = 0;
2610 ops[i].indata = bl;
31f18b77 2611 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2612 CEPH_OSD_FLAG_WRITE, oncommit, objver);
2613 o->mtime = mtime;
2614 o->snapc = snapc;
2615 return o;
2616 }
2617 ceph_tid_t append(
2618 const object_t& oid, const object_locator_t& oloc,
2619 uint64_t len, const SnapContext& snapc,
2620 const bufferlist &bl, ceph::real_time mtime, int flags,
2621 Context *oncommit,
2622 version_t *objver = NULL,
2623 ObjectOperation *extra_ops = NULL) {
2624 Op *o = prepare_append_op(oid, oloc, len, snapc, bl, mtime, flags,
2625 oncommit, objver, extra_ops);
2626 ceph_tid_t tid;
2627 op_submit(o, &tid);
2628 return tid;
2629 }
2630 ceph_tid_t write_trunc(const object_t& oid, const object_locator_t& oloc,
2631 uint64_t off, uint64_t len, const SnapContext& snapc,
2632 const bufferlist &bl, ceph::real_time mtime, int flags,
2633 uint64_t trunc_size, __u32 trunc_seq,
2634 Context *oncommit,
2635 version_t *objver = NULL,
2636 ObjectOperation *extra_ops = NULL, int op_flags = 0) {
2637 vector<OSDOp> ops;
2638 int i = init_ops(ops, 1, extra_ops);
2639 ops[i].op.op = CEPH_OSD_OP_WRITE;
2640 ops[i].op.extent.offset = off;
2641 ops[i].op.extent.length = len;
2642 ops[i].op.extent.truncate_size = trunc_size;
2643 ops[i].op.extent.truncate_seq = trunc_seq;
2644 ops[i].indata = bl;
2645 ops[i].op.flags = op_flags;
31f18b77 2646 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2647 CEPH_OSD_FLAG_WRITE, oncommit, objver);
2648 o->mtime = mtime;
2649 o->snapc = snapc;
2650 ceph_tid_t tid;
2651 op_submit(o, &tid);
2652 return tid;
2653 }
2654 Op *prepare_write_full_op(
2655 const object_t& oid, const object_locator_t& oloc,
2656 const SnapContext& snapc, const bufferlist &bl,
2657 ceph::real_time mtime, int flags,
2658 Context *oncommit, version_t *objver = NULL,
2659 ObjectOperation *extra_ops = NULL, int op_flags = 0) {
2660 vector<OSDOp> ops;
2661 int i = init_ops(ops, 1, extra_ops);
2662 ops[i].op.op = CEPH_OSD_OP_WRITEFULL;
2663 ops[i].op.extent.offset = 0;
2664 ops[i].op.extent.length = bl.length();
2665 ops[i].indata = bl;
2666 ops[i].op.flags = op_flags;
31f18b77 2667 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2668 CEPH_OSD_FLAG_WRITE, oncommit, objver);
2669 o->mtime = mtime;
2670 o->snapc = snapc;
2671 return o;
2672 }
2673 ceph_tid_t write_full(
2674 const object_t& oid, const object_locator_t& oloc,
2675 const SnapContext& snapc, const bufferlist &bl,
2676 ceph::real_time mtime, int flags,
2677 Context *oncommit, version_t *objver = NULL,
2678 ObjectOperation *extra_ops = NULL, int op_flags = 0) {
2679 Op *o = prepare_write_full_op(oid, oloc, snapc, bl, mtime, flags,
2680 oncommit, objver, extra_ops, op_flags);
2681 ceph_tid_t tid;
2682 op_submit(o, &tid);
2683 return tid;
2684 }
2685 Op *prepare_writesame_op(
2686 const object_t& oid, const object_locator_t& oloc,
2687 uint64_t write_len, uint64_t off,
2688 const SnapContext& snapc, const bufferlist &bl,
2689 ceph::real_time mtime, int flags,
2690 Context *oncommit, version_t *objver = NULL,
2691 ObjectOperation *extra_ops = NULL, int op_flags = 0) {
2692
2693 vector<OSDOp> ops;
2694 int i = init_ops(ops, 1, extra_ops);
2695 ops[i].op.op = CEPH_OSD_OP_WRITESAME;
2696 ops[i].op.writesame.offset = off;
2697 ops[i].op.writesame.length = write_len;
2698 ops[i].op.writesame.data_length = bl.length();
2699 ops[i].indata = bl;
2700 ops[i].op.flags = op_flags;
31f18b77 2701 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2702 CEPH_OSD_FLAG_WRITE, oncommit, objver);
2703 o->mtime = mtime;
2704 o->snapc = snapc;
2705 return o;
2706 }
2707 ceph_tid_t writesame(
2708 const object_t& oid, const object_locator_t& oloc,
2709 uint64_t write_len, uint64_t off,
2710 const SnapContext& snapc, const bufferlist &bl,
2711 ceph::real_time mtime, int flags,
2712 Context *oncommit, version_t *objver = NULL,
2713 ObjectOperation *extra_ops = NULL, int op_flags = 0) {
2714
2715 Op *o = prepare_writesame_op(oid, oloc, write_len, off, snapc, bl,
2716 mtime, flags, oncommit, objver,
2717 extra_ops, op_flags);
2718
2719 ceph_tid_t tid;
2720 op_submit(o, &tid);
2721 return tid;
2722 }
2723 ceph_tid_t trunc(const object_t& oid, const object_locator_t& oloc,
2724 const SnapContext& snapc, ceph::real_time mtime, int flags,
2725 uint64_t trunc_size, __u32 trunc_seq,
2726 Context *oncommit, version_t *objver = NULL,
2727 ObjectOperation *extra_ops = NULL) {
2728 vector<OSDOp> ops;
2729 int i = init_ops(ops, 1, extra_ops);
2730 ops[i].op.op = CEPH_OSD_OP_TRUNCATE;
2731 ops[i].op.extent.offset = trunc_size;
2732 ops[i].op.extent.truncate_size = trunc_size;
2733 ops[i].op.extent.truncate_seq = trunc_seq;
31f18b77 2734 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2735 CEPH_OSD_FLAG_WRITE, oncommit, objver);
2736 o->mtime = mtime;
2737 o->snapc = snapc;
2738 ceph_tid_t tid;
2739 op_submit(o, &tid);
2740 return tid;
2741 }
2742 ceph_tid_t zero(const object_t& oid, const object_locator_t& oloc,
2743 uint64_t off, uint64_t len, const SnapContext& snapc,
2744 ceph::real_time mtime, int flags, Context *oncommit,
2745 version_t *objver = NULL, ObjectOperation *extra_ops = NULL) {
2746 vector<OSDOp> ops;
2747 int i = init_ops(ops, 1, extra_ops);
2748 ops[i].op.op = CEPH_OSD_OP_ZERO;
2749 ops[i].op.extent.offset = off;
2750 ops[i].op.extent.length = len;
31f18b77 2751 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2752 CEPH_OSD_FLAG_WRITE, oncommit, objver);
2753 o->mtime = mtime;
2754 o->snapc = snapc;
2755 ceph_tid_t tid;
2756 op_submit(o, &tid);
2757 return tid;
2758 }
2759 ceph_tid_t rollback_object(const object_t& oid, const object_locator_t& oloc,
2760 const SnapContext& snapc, snapid_t snapid,
2761 ceph::real_time mtime, Context *oncommit,
2762 version_t *objver = NULL,
2763 ObjectOperation *extra_ops = NULL) {
2764 vector<OSDOp> ops;
2765 int i = init_ops(ops, 1, extra_ops);
2766 ops[i].op.op = CEPH_OSD_OP_ROLLBACK;
2767 ops[i].op.snap.snapid = snapid;
2768 Op *o = new Op(oid, oloc, ops, CEPH_OSD_FLAG_WRITE, oncommit, objver);
2769 o->mtime = mtime;
2770 o->snapc = snapc;
2771 ceph_tid_t tid;
2772 op_submit(o, &tid);
2773 return tid;
2774 }
2775 ceph_tid_t create(const object_t& oid, const object_locator_t& oloc,
2776 const SnapContext& snapc, ceph::real_time mtime, int global_flags,
2777 int create_flags, Context *oncommit,
2778 version_t *objver = NULL,
2779 ObjectOperation *extra_ops = NULL) {
2780 vector<OSDOp> ops;
2781 int i = init_ops(ops, 1, extra_ops);
2782 ops[i].op.op = CEPH_OSD_OP_CREATE;
2783 ops[i].op.flags = create_flags;
31f18b77 2784 Op *o = new Op(oid, oloc, ops, global_flags | global_op_flags |
7c673cae
FG
2785 CEPH_OSD_FLAG_WRITE, oncommit, objver);
2786 o->mtime = mtime;
2787 o->snapc = snapc;
2788 ceph_tid_t tid;
2789 op_submit(o, &tid);
2790 return tid;
2791 }
2792 Op *prepare_remove_op(
2793 const object_t& oid, const object_locator_t& oloc,
2794 const SnapContext& snapc, ceph::real_time mtime, int flags,
2795 Context *oncommit,
2796 version_t *objver = NULL, ObjectOperation *extra_ops = NULL) {
2797 vector<OSDOp> ops;
2798 int i = init_ops(ops, 1, extra_ops);
2799 ops[i].op.op = CEPH_OSD_OP_DELETE;
31f18b77 2800 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2801 CEPH_OSD_FLAG_WRITE, oncommit, objver);
2802 o->mtime = mtime;
2803 o->snapc = snapc;
2804 return o;
2805 }
2806 ceph_tid_t remove(
2807 const object_t& oid, const object_locator_t& oloc,
2808 const SnapContext& snapc, ceph::real_time mtime, int flags,
2809 Context *oncommit,
2810 version_t *objver = NULL, ObjectOperation *extra_ops = NULL) {
2811 Op *o = prepare_remove_op(oid, oloc, snapc, mtime, flags,
2812 oncommit, objver, extra_ops);
2813 ceph_tid_t tid;
2814 op_submit(o, &tid);
2815 return tid;
2816 }
2817
2818 ceph_tid_t setxattr(const object_t& oid, const object_locator_t& oloc,
2819 const char *name, const SnapContext& snapc, const bufferlist &bl,
2820 ceph::real_time mtime, int flags,
2821 Context *oncommit,
2822 version_t *objver = NULL, ObjectOperation *extra_ops = NULL) {
2823 vector<OSDOp> ops;
2824 int i = init_ops(ops, 1, extra_ops);
2825 ops[i].op.op = CEPH_OSD_OP_SETXATTR;
2826 ops[i].op.xattr.name_len = (name ? strlen(name) : 0);
2827 ops[i].op.xattr.value_len = bl.length();
2828 if (name)
2829 ops[i].indata.append(name);
2830 ops[i].indata.append(bl);
31f18b77 2831 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2832 CEPH_OSD_FLAG_WRITE, oncommit, objver);
2833 o->mtime = mtime;
2834 o->snapc = snapc;
2835 ceph_tid_t tid;
2836 op_submit(o, &tid);
2837 return tid;
2838 }
2839 ceph_tid_t removexattr(const object_t& oid, const object_locator_t& oloc,
2840 const char *name, const SnapContext& snapc,
2841 ceph::real_time mtime, int flags,
2842 Context *oncommit,
2843 version_t *objver = NULL, ObjectOperation *extra_ops = NULL) {
2844 vector<OSDOp> ops;
2845 int i = init_ops(ops, 1, extra_ops);
2846 ops[i].op.op = CEPH_OSD_OP_RMXATTR;
2847 ops[i].op.xattr.name_len = (name ? strlen(name) : 0);
2848 ops[i].op.xattr.value_len = 0;
2849 if (name)
2850 ops[i].indata.append(name);
31f18b77 2851 Op *o = new Op(oid, oloc, ops, flags | global_op_flags |
7c673cae
FG
2852 CEPH_OSD_FLAG_WRITE, oncommit, objver);
2853 o->mtime = mtime;
2854 o->snapc = snapc;
2855 ceph_tid_t tid;
2856 op_submit(o, &tid);
2857 return tid;
2858 }
2859
2860 void list_nobjects(NListContext *p, Context *onfinish);
2861 uint32_t list_nobjects_seek(NListContext *p, uint32_t pos);
2862 uint32_t list_nobjects_seek(NListContext *list_context, const hobject_t& c);
2863 void list_nobjects_get_cursor(NListContext *list_context, hobject_t *c);
2864
2865 hobject_t enumerate_objects_begin();
2866 hobject_t enumerate_objects_end();
2867 //hobject_t enumerate_objects_begin(int n, int m);
2868 void enumerate_objects(
2869 int64_t pool_id,
2870 const std::string &ns,
2871 const hobject_t &start,
2872 const hobject_t &end,
2873 const uint32_t max,
2874 const bufferlist &filter_bl,
2875 std::list<librados::ListObjectImpl> *result,
2876 hobject_t *next,
2877 Context *on_finish);
2878
2879 void _enumerate_reply(
2880 bufferlist &bl,
2881 int r,
2882 const hobject_t &end,
2883 const int64_t pool_id,
2884 int budget,
2885 epoch_t reply_epoch,
2886 std::list<librados::ListObjectImpl> *result,
2887 hobject_t *next,
2888 Context *on_finish);
2889 friend class C_EnumerateReply;
2890
2891 // -------------------------
2892 // pool ops
2893private:
2894 void pool_op_submit(PoolOp *op);
2895 void _pool_op_submit(PoolOp *op);
2896 void _finish_pool_op(PoolOp *op, int r);
2897 void _do_delete_pool(int64_t pool, Context *onfinish);
2898public:
2899 int create_pool_snap(int64_t pool, string& snapName, Context *onfinish);
2900 int allocate_selfmanaged_snap(int64_t pool, snapid_t *psnapid,
2901 Context *onfinish);
2902 int delete_pool_snap(int64_t pool, string& snapName, Context *onfinish);
2903 int delete_selfmanaged_snap(int64_t pool, snapid_t snap, Context *onfinish);
2904
2905 int create_pool(string& name, Context *onfinish, uint64_t auid=0,
2906 int crush_rule=-1);
2907 int delete_pool(int64_t pool, Context *onfinish);
2908 int delete_pool(const string& name, Context *onfinish);
2909 int change_pool_auid(int64_t pool, Context *onfinish, uint64_t auid);
2910
2911 void handle_pool_op_reply(MPoolOpReply *m);
2912 int pool_op_cancel(ceph_tid_t tid, int r);
2913
2914 // --------------------------
2915 // pool stats
2916private:
2917 void _poolstat_submit(PoolStatOp *op);
2918public:
2919 void handle_get_pool_stats_reply(MGetPoolStatsReply *m);
2920 void get_pool_stats(list<string>& pools, map<string,pool_stat_t> *result,
2921 Context *onfinish);
2922 int pool_stat_op_cancel(ceph_tid_t tid, int r);
2923 void _finish_pool_stat_op(PoolStatOp *op, int r);
2924
2925 // ---------------------------
2926 // df stats
2927private:
2928 void _fs_stats_submit(StatfsOp *op);
2929public:
2930 void handle_fs_stats_reply(MStatfsReply *m);
2931 void get_fs_stats(struct ceph_statfs& result, Context *onfinish);
2932 int statfs_op_cancel(ceph_tid_t tid, int r);
2933 void _finish_statfs_op(StatfsOp *op, int r);
2934
2935 // ---------------------------
2936 // some scatter/gather hackery
2937
2938 void _sg_read_finish(vector<ObjectExtent>& extents,
2939 vector<bufferlist>& resultbl,
2940 bufferlist *bl, Context *onfinish);
2941
2942 struct C_SGRead : public Context {
2943 Objecter *objecter;
2944 vector<ObjectExtent> extents;
2945 vector<bufferlist> resultbl;
2946 bufferlist *bl;
2947 Context *onfinish;
2948 C_SGRead(Objecter *ob,
2949 vector<ObjectExtent>& e, vector<bufferlist>& r, bufferlist *b,
2950 Context *c) :
2951 objecter(ob), bl(b), onfinish(c) {
2952 extents.swap(e);
2953 resultbl.swap(r);
2954 }
2955 void finish(int r) override {
2956 objecter->_sg_read_finish(extents, resultbl, bl, onfinish);
2957 }
2958 };
2959
2960 void sg_read_trunc(vector<ObjectExtent>& extents, snapid_t snap,
2961 bufferlist *bl, int flags, uint64_t trunc_size,
2962 __u32 trunc_seq, Context *onfinish, int op_flags = 0) {
2963 if (extents.size() == 1) {
2964 read_trunc(extents[0].oid, extents[0].oloc, extents[0].offset,
2965 extents[0].length, snap, bl, flags, extents[0].truncate_size,
2966 trunc_seq, onfinish, 0, 0, op_flags);
2967 } else {
2968 C_GatherBuilder gather(cct);
2969 vector<bufferlist> resultbl(extents.size());
2970 int i=0;
2971 for (vector<ObjectExtent>::iterator p = extents.begin();
2972 p != extents.end();
2973 ++p) {
2974 read_trunc(p->oid, p->oloc, p->offset, p->length, snap, &resultbl[i++],
2975 flags, p->truncate_size, trunc_seq, gather.new_sub(),
2976 0, 0, op_flags);
2977 }
2978 gather.set_finisher(new C_SGRead(this, extents, resultbl, bl, onfinish));
2979 gather.activate();
2980 }
2981 }
2982
2983 void sg_read(vector<ObjectExtent>& extents, snapid_t snap, bufferlist *bl,
2984 int flags, Context *onfinish, int op_flags = 0) {
2985 sg_read_trunc(extents, snap, bl, flags, 0, 0, onfinish, op_flags);
2986 }
2987
2988 void sg_write_trunc(vector<ObjectExtent>& extents, const SnapContext& snapc,
2989 const bufferlist& bl, ceph::real_time mtime, int flags,
2990 uint64_t trunc_size, __u32 trunc_seq,
2991 Context *oncommit, int op_flags = 0) {
2992 if (extents.size() == 1) {
2993 write_trunc(extents[0].oid, extents[0].oloc, extents[0].offset,
2994 extents[0].length, snapc, bl, mtime, flags,
2995 extents[0].truncate_size, trunc_seq, oncommit,
2996 0, 0, op_flags);
2997 } else {
2998 C_GatherBuilder gcom(cct, oncommit);
2999 for (vector<ObjectExtent>::iterator p = extents.begin();
3000 p != extents.end();
3001 ++p) {
3002 bufferlist cur;
3003 for (vector<pair<uint64_t,uint64_t> >::iterator bit
3004 = p->buffer_extents.begin();
3005 bit != p->buffer_extents.end();
3006 ++bit)
3007 bl.copy(bit->first, bit->second, cur);
3008 assert(cur.length() == p->length);
3009 write_trunc(p->oid, p->oloc, p->offset, p->length,
3010 snapc, cur, mtime, flags, p->truncate_size, trunc_seq,
3011 oncommit ? gcom.new_sub():0,
3012 0, 0, op_flags);
3013 }
3014 gcom.activate();
3015 }
3016 }
3017
3018 void sg_write(vector<ObjectExtent>& extents, const SnapContext& snapc,
3019 const bufferlist& bl, ceph::real_time mtime, int flags,
3020 Context *oncommit, int op_flags = 0) {
3021 sg_write_trunc(extents, snapc, bl, mtime, flags, 0, 0, oncommit,
3022 op_flags);
3023 }
3024
3025 void ms_handle_connect(Connection *con) override;
3026 bool ms_handle_reset(Connection *con) override;
3027 void ms_handle_remote_reset(Connection *con) override;
3028 bool ms_handle_refused(Connection *con) override;
3029 bool ms_get_authorizer(int dest_type,
3030 AuthAuthorizer **authorizer,
3031 bool force_new) override;
3032
3033 void blacklist_self(bool set);
3034
3035private:
3036 epoch_t epoch_barrier;
3037 bool retry_writes_after_first_reply;
3038public:
3039 void set_epoch_barrier(epoch_t epoch);
3040
3041 PerfCounters *get_logger() {
3042 return logger;
3043 }
3044};
3045
3046#endif