]> git.proxmox.com Git - ceph.git/blob - ceph/src/mds/CInode.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / mds / CInode.h
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
16
17 #ifndef CEPH_CINODE_H
18 #define CEPH_CINODE_H
19
20 #include <list>
21 #include <map>
22 #include <set>
23 #include <string_view>
24
25 #include "common/config.h"
26 #include "include/counter.h"
27 #include "include/elist.h"
28 #include "include/types.h"
29 #include "include/lru.h"
30 #include "include/compact_set.h"
31
32 #include "MDSCacheObject.h"
33 #include "MDSContext.h"
34 #include "flock.h"
35
36 #include "CDentry.h"
37 #include "SimpleLock.h"
38 #include "ScatterLock.h"
39 #include "LocalLock.h"
40 #include "Capability.h"
41 #include "SnapRealm.h"
42 #include "Mutation.h"
43
44 #include "messages/MClientCaps.h"
45
46 #define dout_context g_ceph_context
47
48 class Context;
49 class CDentry;
50 class CDir;
51 class CInode;
52 class MDCache;
53 class LogSegment;
54 struct SnapRealm;
55 class Session;
56 struct ObjectOperation;
57 class EMetaBlob;
58
59
60 ostream& operator<<(ostream& out, const CInode& in);
61
62 struct cinode_lock_info_t {
63 int lock;
64 int wr_caps;
65 };
66
67 extern cinode_lock_info_t cinode_lock_info[];
68 extern int num_cinode_locks;
69
70
71 /**
72 * Base class for CInode, containing the backing store data and
73 * serialization methods. This exists so that we can read and
74 * handle CInodes from the backing store without hitting all
75 * the business logic in CInode proper.
76 */
77 class InodeStoreBase {
78 public:
79 typedef inode_t<mempool::mds_co::pool_allocator> mempool_inode;
80 typedef old_inode_t<mempool::mds_co::pool_allocator> mempool_old_inode;
81 typedef mempool::mds_co::compact_map<snapid_t, mempool_old_inode> mempool_old_inode_map;
82 typedef xattr_map<mempool::mds_co::pool_allocator> mempool_xattr_map; // FIXME bufferptr not in mempool
83
84 mempool_inode inode; // the inode itself
85 mempool::mds_co::string symlink; // symlink dest, if symlink
86 mempool_xattr_map xattrs;
87 fragtree_t dirfragtree; // dir frag tree, if any. always consistent with our dirfrag map.
88 mempool_old_inode_map old_inodes; // key = last, value.first = first
89 snapid_t oldest_snap = CEPH_NOSNAP;
90 damage_flags_t damage_flags = 0;
91
92 InodeStoreBase() {}
93
94 /* Helpers */
95 bool is_file() const { return inode.is_file(); }
96 bool is_symlink() const { return inode.is_symlink(); }
97 bool is_dir() const { return inode.is_dir(); }
98 static object_t get_object_name(inodeno_t ino, frag_t fg, std::string_view suffix);
99
100 /* Full serialization for use in ".inode" root inode objects */
101 void encode(bufferlist &bl, uint64_t features, const bufferlist *snap_blob=NULL) const;
102 void decode(bufferlist::const_iterator &bl, bufferlist& snap_blob);
103
104 /* Serialization without ENCODE_START/FINISH blocks for use embedded in dentry */
105 void encode_bare(bufferlist &bl, uint64_t features, const bufferlist *snap_blob=NULL) const;
106 void decode_bare(bufferlist::const_iterator &bl, bufferlist &snap_blob, __u8 struct_v=5);
107
108 /* For test/debug output */
109 void dump(Formatter *f) const;
110
111 /* For use by offline tools */
112 __u32 hash_dentry_name(std::string_view dn);
113 frag_t pick_dirfrag(std::string_view dn);
114 };
115
116 class InodeStore : public InodeStoreBase {
117 public:
118 // FIXME bufferlist not part of mempool
119 bufferlist snap_blob; // Encoded copy of SnapRealm, because we can't
120 // rehydrate it without full MDCache
121 void encode(bufferlist &bl, uint64_t features) const {
122 InodeStoreBase::encode(bl, features, &snap_blob);
123 }
124 void decode(bufferlist::const_iterator &bl) {
125 InodeStoreBase::decode(bl, snap_blob);
126 }
127 void encode_bare(bufferlist &bl, uint64_t features) const {
128 InodeStoreBase::encode_bare(bl, features, &snap_blob);
129 }
130 void decode_bare(bufferlist::const_iterator &bl) {
131 InodeStoreBase::decode_bare(bl, snap_blob);
132 }
133
134 static void generate_test_instances(std::list<InodeStore*>& ls);
135 };
136 WRITE_CLASS_ENCODER_FEATURES(InodeStore)
137
138 // just for ceph-dencoder
139 class InodeStoreBare : public InodeStore {
140 public:
141 void encode(bufferlist &bl, uint64_t features) const {
142 InodeStore::encode_bare(bl, features);
143 }
144 void decode(bufferlist::const_iterator &bl) {
145 InodeStore::decode_bare(bl);
146 }
147 static void generate_test_instances(std::list<InodeStoreBare*>& ls);
148 };
149 WRITE_CLASS_ENCODER_FEATURES(InodeStoreBare)
150
151 // cached inode wrapper
152 class CInode : public MDSCacheObject, public InodeStoreBase, public Counter<CInode> {
153 public:
154 MEMPOOL_CLASS_HELPERS();
155 // -- pins --
156 static const int PIN_DIRFRAG = -1;
157 static const int PIN_CAPS = 2; // client caps
158 static const int PIN_IMPORTING = -4; // importing
159 static const int PIN_OPENINGDIR = 7;
160 static const int PIN_REMOTEPARENT = 8;
161 static const int PIN_BATCHOPENJOURNAL = 9;
162 static const int PIN_SCATTERED = 10;
163 static const int PIN_STICKYDIRS = 11;
164 //static const int PIN_PURGING = -12;
165 static const int PIN_FREEZING = 13;
166 static const int PIN_FROZEN = 14;
167 static const int PIN_IMPORTINGCAPS = -15;
168 static const int PIN_PASTSNAPPARENT = -16;
169 static const int PIN_OPENINGSNAPPARENTS = 17;
170 static const int PIN_TRUNCATING = 18;
171 static const int PIN_STRAY = 19; // we pin our stray inode while active
172 static const int PIN_NEEDSNAPFLUSH = 20;
173 static const int PIN_DIRTYRSTAT = 21;
174 static const int PIN_EXPORTINGCAPS = 22;
175 static const int PIN_DIRTYPARENT = 23;
176 static const int PIN_DIRWAITER = 24;
177 static const int PIN_SCRUBQUEUE = 25;
178
179 std::string_view pin_name(int p) const override {
180 switch (p) {
181 case PIN_DIRFRAG: return "dirfrag";
182 case PIN_CAPS: return "caps";
183 case PIN_IMPORTING: return "importing";
184 case PIN_OPENINGDIR: return "openingdir";
185 case PIN_REMOTEPARENT: return "remoteparent";
186 case PIN_BATCHOPENJOURNAL: return "batchopenjournal";
187 case PIN_SCATTERED: return "scattered";
188 case PIN_STICKYDIRS: return "stickydirs";
189 //case PIN_PURGING: return "purging";
190 case PIN_FREEZING: return "freezing";
191 case PIN_FROZEN: return "frozen";
192 case PIN_IMPORTINGCAPS: return "importingcaps";
193 case PIN_EXPORTINGCAPS: return "exportingcaps";
194 case PIN_PASTSNAPPARENT: return "pastsnapparent";
195 case PIN_OPENINGSNAPPARENTS: return "openingsnapparents";
196 case PIN_TRUNCATING: return "truncating";
197 case PIN_STRAY: return "stray";
198 case PIN_NEEDSNAPFLUSH: return "needsnapflush";
199 case PIN_DIRTYRSTAT: return "dirtyrstat";
200 case PIN_DIRTYPARENT: return "dirtyparent";
201 case PIN_DIRWAITER: return "dirwaiter";
202 case PIN_SCRUBQUEUE: return "scrubqueue";
203 default: return generic_pin_name(p);
204 }
205 }
206
207 // -- dump flags --
208 static const int DUMP_INODE_STORE_BASE = (1 << 0);
209 static const int DUMP_MDS_CACHE_OBJECT = (1 << 1);
210 static const int DUMP_LOCKS = (1 << 2);
211 static const int DUMP_STATE = (1 << 3);
212 static const int DUMP_CAPS = (1 << 4);
213 static const int DUMP_PATH = (1 << 5);
214 static const int DUMP_DIRFRAGS = (1 << 6);
215 static const int DUMP_ALL = (-1);
216 static const int DUMP_DEFAULT = DUMP_ALL & (~DUMP_PATH) & (~DUMP_DIRFRAGS);
217
218 // -- state --
219 static const int STATE_EXPORTING = (1<<0); // on nonauth bystander.
220 static const int STATE_OPENINGDIR = (1<<1);
221 static const int STATE_FREEZING = (1<<2);
222 static const int STATE_FROZEN = (1<<3);
223 static const int STATE_AMBIGUOUSAUTH = (1<<4);
224 static const int STATE_EXPORTINGCAPS = (1<<5);
225 static const int STATE_NEEDSRECOVER = (1<<6);
226 static const int STATE_RECOVERING = (1<<7);
227 static const int STATE_PURGING = (1<<8);
228 static const int STATE_DIRTYPARENT = (1<<9);
229 static const int STATE_DIRTYRSTAT = (1<<10);
230 static const int STATE_STRAYPINNED = (1<<11);
231 static const int STATE_FROZENAUTHPIN = (1<<12);
232 static const int STATE_DIRTYPOOL = (1<<13);
233 static const int STATE_REPAIRSTATS = (1<<14);
234 static const int STATE_MISSINGOBJS = (1<<15);
235 static const int STATE_EVALSTALECAPS = (1<<16);
236 static const int STATE_QUEUEDEXPORTPIN = (1<<17);
237 static const int STATE_TRACKEDBYOFT = (1<<18); // tracked by open file table
238 // orphan inode needs notification of releasing reference
239 static const int STATE_ORPHAN = STATE_NOTIFYREF;
240
241 static const int MASK_STATE_EXPORTED =
242 (STATE_DIRTY|STATE_NEEDSRECOVER|STATE_DIRTYPARENT|STATE_DIRTYPOOL);
243 static const int MASK_STATE_EXPORT_KEPT =
244 (STATE_FROZEN|STATE_AMBIGUOUSAUTH|STATE_EXPORTINGCAPS|
245 STATE_QUEUEDEXPORTPIN|STATE_TRACKEDBYOFT);
246
247 // -- waiters --
248 static const uint64_t WAIT_DIR = (1<<0);
249 static const uint64_t WAIT_FROZEN = (1<<1);
250 static const uint64_t WAIT_TRUNC = (1<<2);
251 static const uint64_t WAIT_FLOCK = (1<<3);
252
253 static const uint64_t WAIT_ANY_MASK = (uint64_t)(-1);
254
255 // misc
256 static const unsigned EXPORT_NONCE = 1; // nonce given to replicas created by export
257
258 ostream& print_db_line_prefix(ostream& out) override;
259
260 public:
261 MDCache *mdcache;
262
263 SnapRealm *snaprealm = nullptr;
264 SnapRealm *containing_realm = nullptr;
265 snapid_t first, last;
266 mempool::mds_co::compact_set<snapid_t> dirty_old_rstats;
267
268 class scrub_stamp_info_t {
269 public:
270 /// version we started our latest scrub (whether in-progress or finished)
271 version_t scrub_start_version = 0;
272 /// time we started our latest scrub (whether in-progress or finished)
273 utime_t scrub_start_stamp;
274 /// version we started our most recent finished scrub
275 version_t last_scrub_version = 0;
276 /// time we started our most recent finished scrub
277 utime_t last_scrub_stamp;
278 scrub_stamp_info_t() {}
279 void reset() {
280 scrub_start_version = last_scrub_version = 0;
281 scrub_start_stamp = last_scrub_stamp = utime_t();
282 }
283 };
284
285 class scrub_info_t : public scrub_stamp_info_t {
286 public:
287 CDentry *scrub_parent = nullptr;
288 MDSContext *on_finish = nullptr;
289
290 bool last_scrub_dirty = false; /// are our stamps dirty with respect to disk state?
291 bool scrub_in_progress = false; /// are we currently scrubbing?
292 bool children_scrubbed = false;
293
294 /// my own (temporary) stamps and versions for each dirfrag we have
295 std::map<frag_t, scrub_stamp_info_t> dirfrag_stamps; // XXX not part of mempool
296
297 ScrubHeaderRef header;
298
299 scrub_info_t() {}
300 };
301
302 const scrub_info_t *scrub_info() const{
303 if (!scrub_infop)
304 scrub_info_create();
305 return scrub_infop;
306 }
307
308 ScrubHeaderRef get_scrub_header() {
309 if (scrub_infop == nullptr) {
310 return nullptr;
311 } else {
312 return scrub_infop->header;
313 }
314 }
315
316 bool scrub_is_in_progress() const {
317 return (scrub_infop && scrub_infop->scrub_in_progress);
318 }
319 /**
320 * Start scrubbing on this inode. That could be very short if it's
321 * a file, or take a long time if we're recursively scrubbing a directory.
322 * @pre It is not currently scrubbing
323 * @post it has set up internal scrubbing state
324 * @param scrub_version What version are we scrubbing at (usually, parent
325 * directory's get_projected_version())
326 */
327 void scrub_initialize(CDentry *scrub_parent,
328 ScrubHeaderRef& header,
329 MDSContext *f);
330 /**
331 * Get the next dirfrag to scrub. Gives you a frag_t in output param which
332 * you must convert to a CDir (and possibly load off disk).
333 * @param dir A pointer to frag_t, will be filled in with the next dirfrag to
334 * scrub if there is one.
335 * @returns 0 on success, you should scrub the passed-out frag_t right now;
336 * ENOENT: There are no remaining dirfrags to scrub
337 * <0 There was some other error (It will return -ENOTDIR if not a directory)
338 */
339 int scrub_dirfrag_next(frag_t* out_dirfrag);
340 /**
341 * Get the currently scrubbing dirfrags. When returned, the
342 * passed-in list will be filled in with all frag_ts which have
343 * been returned from scrub_dirfrag_next but not sent back
344 * via scrub_dirfrag_finished.
345 */
346 void scrub_dirfrags_scrubbing(frag_vec_t *out_dirfrags);
347 /**
348 * Report to the CInode that a dirfrag it owns has been scrubbed. Call
349 * this for every frag_t returned from scrub_dirfrag_next().
350 * @param dirfrag The frag_t that was scrubbed
351 */
352 void scrub_dirfrag_finished(frag_t dirfrag);
353 /**
354 * Call this once the scrub has been completed, whether it's a full
355 * recursive scrub on a directory or simply the data on a file (or
356 * anything in between).
357 * @param c An out param which is filled in with a Context* that must
358 * be complete()ed.
359 */
360 void scrub_finished(MDSContext **c);
361
362 void scrub_aborted(MDSContext **c);
363
364 /**
365 * Report to the CInode that alldirfrags it owns have been scrubbed.
366 */
367 void scrub_children_finished() {
368 scrub_infop->children_scrubbed = true;
369 }
370 void scrub_set_finisher(MDSContext *c) {
371 ceph_assert(!scrub_infop->on_finish);
372 scrub_infop->on_finish = c;
373 }
374
375 private:
376 /**
377 * Create a scrub_info_t struct for the scrub_infop pointer.
378 */
379 void scrub_info_create() const;
380 /**
381 * Delete the scrub_info_t struct if it's not got any useful data
382 */
383 void scrub_maybe_delete_info();
384 public:
385
386 bool is_multiversion() const {
387 return snaprealm || // other snaprealms will link to me
388 inode.is_dir() || // links to me in other snaps
389 inode.nlink > 1 || // there are remote links, possibly snapped, that will need to find me
390 !old_inodes.empty(); // once multiversion, always multiversion. until old_inodes gets cleaned out.
391 }
392 snapid_t get_oldest_snap();
393
394 uint64_t last_journaled = 0; // log offset for the last time i was journaled
395 //loff_t last_open_journaled; // log offset for the last journaled EOpen
396 utime_t last_dirstat_prop;
397
398
399 // list item node for when we have unpropagated rstat data
400 elist<CInode*>::item dirty_rstat_item;
401
402 bool is_dirty_rstat() {
403 return state_test(STATE_DIRTYRSTAT);
404 }
405 void mark_dirty_rstat();
406 void clear_dirty_rstat();
407
408 //bool hack_accessed = false;
409 //utime_t hack_load_stamp;
410
411 /**
412 * Projection methods, used to store inode changes until they have been journaled,
413 * at which point they are popped.
414 * Usage:
415 * project_inode as needed. If you're changing xattrs or sr_t, then pass true
416 * as needed then change the xattrs/snapnode member as needed. (Dirty
417 * exception: project_past_snaprealm_parent allows you to project the
418 * snapnode after doing project_inode (i.e. you don't need to pass
419 * snap=true).
420 *
421 * Then, journal. Once journaling is done, pop_and_dirty_projected_inode.
422 * This function will take care of the inode itself, the xattrs, and the snaprealm.
423 */
424
425 class projected_inode {
426 public:
427 static sr_t* const UNDEF_SRNODE;
428
429 mempool_inode inode;
430 std::unique_ptr<mempool_xattr_map> xattrs;
431 sr_t *snapnode = UNDEF_SRNODE;
432
433 projected_inode() = delete;
434 explicit projected_inode(const mempool_inode &in) : inode(in) {}
435 };
436
437 private:
438 mempool::mds_co::list<projected_inode> projected_nodes; // projected values (only defined while dirty)
439 size_t num_projected_xattrs = 0;
440 size_t num_projected_srnodes = 0;
441
442 public:
443 CInode::projected_inode &project_inode(bool xattr = false, bool snap = false);
444 void pop_and_dirty_projected_inode(LogSegment *ls);
445
446 projected_inode *get_projected_node() {
447 if (projected_nodes.empty())
448 return NULL;
449 else
450 return &projected_nodes.back();
451 }
452
453 version_t get_projected_version() const {
454 if (projected_nodes.empty())
455 return inode.version;
456 else
457 return projected_nodes.back().inode.version;
458 }
459 bool is_projected() const {
460 return !projected_nodes.empty();
461 }
462
463 const mempool_inode *get_projected_inode() const {
464 if (projected_nodes.empty())
465 return &inode;
466 else
467 return &projected_nodes.back().inode;
468 }
469 mempool_inode *get_projected_inode() {
470 if (projected_nodes.empty())
471 return &inode;
472 else
473 return &projected_nodes.back().inode;
474 }
475 mempool_inode *get_previous_projected_inode() {
476 ceph_assert(!projected_nodes.empty());
477 auto it = projected_nodes.rbegin();
478 ++it;
479 if (it != projected_nodes.rend())
480 return &it->inode;
481 else
482 return &inode;
483 }
484
485 mempool_xattr_map *get_projected_xattrs() {
486 if (num_projected_xattrs > 0) {
487 for (auto it = projected_nodes.rbegin(); it != projected_nodes.rend(); ++it)
488 if (it->xattrs)
489 return it->xattrs.get();
490 }
491 return &xattrs;
492 }
493 mempool_xattr_map *get_previous_projected_xattrs() {
494 if (num_projected_xattrs > 0) {
495 for (auto it = ++projected_nodes.rbegin(); it != projected_nodes.rend(); ++it)
496 if (it->xattrs)
497 return it->xattrs.get();
498 }
499 return &xattrs;
500 }
501
502 sr_t *prepare_new_srnode(snapid_t snapid);
503 void project_snaprealm(sr_t *new_srnode);
504 sr_t *project_snaprealm(snapid_t snapid=0) {
505 sr_t* new_srnode = prepare_new_srnode(snapid);
506 project_snaprealm(new_srnode);
507 return new_srnode;
508 }
509 const sr_t *get_projected_srnode() const {
510 if (num_projected_srnodes > 0) {
511 for (auto it = projected_nodes.rbegin(); it != projected_nodes.rend(); ++it)
512 if (it->snapnode != projected_inode::UNDEF_SRNODE)
513 return it->snapnode;
514 }
515 if (snaprealm)
516 return &snaprealm->srnode;
517 else
518 return NULL;
519 }
520
521 void mark_snaprealm_global(sr_t *new_srnode);
522 void clear_snaprealm_global(sr_t *new_srnode);
523 bool is_projected_snaprealm_global() const;
524
525 void record_snaprealm_past_parent(sr_t *new_snap, SnapRealm *newparent);
526 void record_snaprealm_parent_dentry(sr_t *new_snap, SnapRealm *newparent,
527 CDentry *dn, bool primary_dn);
528 void project_snaprealm_past_parent(SnapRealm *newparent);
529 void early_pop_projected_snaprealm();
530
531 private:
532 void pop_projected_snaprealm(sr_t *next_snaprealm, bool early);
533
534 public:
535 mempool_old_inode& cow_old_inode(snapid_t follows, bool cow_head);
536 void split_old_inode(snapid_t snap);
537 mempool_old_inode *pick_old_inode(snapid_t last);
538 void pre_cow_old_inode();
539 bool has_snap_data(snapid_t s);
540 void purge_stale_snap_data(const std::set<snapid_t>& snaps);
541
542 // -- cache infrastructure --
543 private:
544 mempool::mds_co::compact_map<frag_t,CDir*> dirfrags; // cached dir fragments under this Inode
545
546 //for the purpose of quickly determining whether there's a subtree root or exporting dir
547 int num_subtree_roots = 0;
548 int num_exporting_dirs = 0;
549
550 int stickydir_ref = 0;
551 scrub_info_t *scrub_infop = nullptr;
552
553 public:
554 bool has_dirfrags() { return !dirfrags.empty(); }
555 CDir* get_dirfrag(frag_t fg) {
556 auto pi = dirfrags.find(fg);
557 if (pi != dirfrags.end()) {
558 //assert(g_conf()->debug_mds < 2 || dirfragtree.is_leaf(fg)); // performance hack FIXME
559 return pi->second;
560 }
561 return NULL;
562 }
563 bool get_dirfrags_under(frag_t fg, std::list<CDir*>& ls);
564 CDir* get_approx_dirfrag(frag_t fg);
565
566 template<typename Container>
567 void get_dirfrags(Container& ls) const {
568 // all dirfrags
569 if constexpr (std::is_same_v<Container, std::vector<CDir*>>)
570 ls.reserve(ls.size() + dirfrags.size());
571 for (const auto &p : dirfrags)
572 ls.push_back(p.second);
573 }
574 template<typename Container>
575 void get_nested_dirfrags(Container& ls) const {
576 // dirfrags in same subtree
577 if constexpr (std::is_same_v<Container, std::vector<CDir*>>)
578 ls.reserve(ls.size() + dirfrags.size() - num_subtree_roots);
579 for (const auto &p : dirfrags) {
580 typename Container::value_type dir = p.second;
581 if (!dir->is_subtree_root())
582 ls.push_back(dir);
583 }
584 }
585 template<typename Container>
586 void get_subtree_dirfrags(Container& ls) {
587 // dirfrags that are roots of new subtrees
588 if constexpr (std::is_same_v<Container, std::vector<CDir*>>)
589 ls.reserve(ls.size() + num_subtree_roots);
590 for (const auto &p : dirfrags) {
591 typename Container::value_type dir = p.second;
592 if (dir->is_subtree_root())
593 ls.push_back(dir);
594 }
595 }
596
597 CDir *get_or_open_dirfrag(MDCache *mdcache, frag_t fg);
598 CDir *add_dirfrag(CDir *dir);
599 void close_dirfrag(frag_t fg);
600 void close_dirfrags();
601 bool has_subtree_root_dirfrag(int auth=-1);
602 bool has_subtree_or_exporting_dirfrag();
603
604 void force_dirfrags();
605 void verify_dirfrags();
606
607 void get_stickydirs();
608 void put_stickydirs();
609
610 protected:
611 // parent dentries in cache
612 CDentry *parent = nullptr; // primary link
613 mempool::mds_co::compact_set<CDentry*> remote_parents; // if hard linked
614
615 mempool::mds_co::list<CDentry*> projected_parent; // for in-progress rename, (un)link, etc.
616
617 mds_authority_t inode_auth = CDIR_AUTH_DEFAULT;
618
619 // -- distributed state --
620 protected:
621 // file capabilities
622 using mempool_cap_map = mempool::mds_co::map<client_t, Capability>;
623 mempool_cap_map client_caps; // client -> caps
624 mempool::mds_co::compact_map<int32_t, int32_t> mds_caps_wanted; // [auth] mds -> caps wanted
625 int replica_caps_wanted = 0; // [replica] what i've requested from auth
626 int num_caps_wanted = 0;
627
628 public:
629 mempool::mds_co::compact_map<int, mempool::mds_co::set<client_t> > client_snap_caps; // [auth] [snap] dirty metadata we still need from the head
630 mempool::mds_co::compact_map<snapid_t, mempool::mds_co::set<client_t> > client_need_snapflush;
631
632 void add_need_snapflush(CInode *snapin, snapid_t snapid, client_t client);
633 void remove_need_snapflush(CInode *snapin, snapid_t snapid, client_t client);
634 bool split_need_snapflush(CInode *cowin, CInode *in);
635
636 protected:
637
638 ceph_lock_state_t *fcntl_locks = nullptr;
639 ceph_lock_state_t *flock_locks = nullptr;
640
641 ceph_lock_state_t *get_fcntl_lock_state() {
642 if (!fcntl_locks)
643 fcntl_locks = new ceph_lock_state_t(g_ceph_context, CEPH_LOCK_FCNTL);
644 return fcntl_locks;
645 }
646 void clear_fcntl_lock_state() {
647 delete fcntl_locks;
648 fcntl_locks = NULL;
649 }
650 ceph_lock_state_t *get_flock_lock_state() {
651 if (!flock_locks)
652 flock_locks = new ceph_lock_state_t(g_ceph_context, CEPH_LOCK_FLOCK);
653 return flock_locks;
654 }
655 void clear_flock_lock_state() {
656 delete flock_locks;
657 flock_locks = NULL;
658 }
659 void clear_file_locks() {
660 clear_fcntl_lock_state();
661 clear_flock_lock_state();
662 }
663 void _encode_file_locks(bufferlist& bl) const {
664 using ceph::encode;
665 bool has_fcntl_locks = fcntl_locks && !fcntl_locks->empty();
666 encode(has_fcntl_locks, bl);
667 if (has_fcntl_locks)
668 encode(*fcntl_locks, bl);
669 bool has_flock_locks = flock_locks && !flock_locks->empty();
670 encode(has_flock_locks, bl);
671 if (has_flock_locks)
672 encode(*flock_locks, bl);
673 }
674 void _decode_file_locks(bufferlist::const_iterator& p) {
675 using ceph::decode;
676 bool has_fcntl_locks;
677 decode(has_fcntl_locks, p);
678 if (has_fcntl_locks)
679 decode(*get_fcntl_lock_state(), p);
680 else
681 clear_fcntl_lock_state();
682 bool has_flock_locks;
683 decode(has_flock_locks, p);
684 if (has_flock_locks)
685 decode(*get_flock_lock_state(), p);
686 else
687 clear_flock_lock_state();
688 }
689
690 // LogSegment lists i (may) belong to
691 public:
692 elist<CInode*>::item item_dirty;
693 elist<CInode*>::item item_caps;
694 elist<CInode*>::item item_open_file;
695 elist<CInode*>::item item_dirty_parent;
696 elist<CInode*>::item item_dirty_dirfrag_dir;
697 elist<CInode*>::item item_dirty_dirfrag_nest;
698 elist<CInode*>::item item_dirty_dirfrag_dirfragtree;
699 elist<CInode*>::item item_scrub;
700
701 // also update RecoveryQueue::RecoveryQueue() if you change this
702 elist<CInode*>::item& item_recover_queue = item_dirty_dirfrag_dir;
703 elist<CInode*>::item& item_recover_queue_front = item_dirty_dirfrag_nest;
704
705 public:
706 int auth_pin_freeze_allowance = 0;
707
708 inode_load_vec_t pop;
709 elist<CInode*>::item item_pop_lru;
710
711 // friends
712 friend class Server;
713 friend class Locker;
714 friend class Migrator;
715 friend class MDCache;
716 friend class StrayManager;
717 friend class CDir;
718 friend class CInodeExport;
719
720 // ---------------------------
721 CInode() = delete;
722 CInode(MDCache *c, bool auth=true, snapid_t f=2, snapid_t l=CEPH_NOSNAP);
723 ~CInode() override {
724 close_dirfrags();
725 close_snaprealm();
726 clear_file_locks();
727 ceph_assert(num_projected_xattrs == 0);
728 ceph_assert(num_projected_srnodes == 0);
729 ceph_assert(num_caps_wanted == 0);
730 ceph_assert(num_subtree_roots == 0);
731 ceph_assert(num_exporting_dirs == 0);
732 }
733
734
735 // -- accessors --
736 bool is_root() const { return inode.ino == MDS_INO_ROOT; }
737 bool is_stray() const { return MDS_INO_IS_STRAY(inode.ino); }
738 mds_rank_t get_stray_owner() const {
739 return (mds_rank_t)MDS_INO_STRAY_OWNER(inode.ino);
740 }
741 bool is_mdsdir() const { return MDS_INO_IS_MDSDIR(inode.ino); }
742 bool is_base() const { return MDS_INO_IS_BASE(inode.ino); }
743 bool is_system() const { return inode.ino < MDS_INO_SYSTEM_BASE; }
744 bool is_normal() const { return !(is_base() || is_system() || is_stray()); }
745
746 bool is_head() const { return last == CEPH_NOSNAP; }
747
748 // note: this overloads MDSCacheObject
749 bool is_ambiguous_auth() const {
750 return state_test(STATE_AMBIGUOUSAUTH) ||
751 MDSCacheObject::is_ambiguous_auth();
752 }
753 void set_ambiguous_auth() {
754 state_set(STATE_AMBIGUOUSAUTH);
755 }
756 void clear_ambiguous_auth(MDSContext::vec& finished);
757 void clear_ambiguous_auth();
758
759 inodeno_t ino() const { return inode.ino; }
760 vinodeno_t vino() const { return vinodeno_t(inode.ino, last); }
761 int d_type() const { return IFTODT(inode.mode); }
762
763 mempool_inode& get_inode() { return inode; }
764 const mempool_inode& get_inode() const { return inode; }
765 CDentry* get_parent_dn() { return parent; }
766 const CDentry* get_parent_dn() const { return parent; }
767 CDentry* get_projected_parent_dn() { return !projected_parent.empty() ? projected_parent.back() : parent; }
768 const CDentry* get_projected_parent_dn() const { return !projected_parent.empty() ? projected_parent.back() : parent; }
769 const CDentry* get_oldest_parent_dn() const {
770 if (parent)
771 return parent;
772 return !projected_parent.empty() ? projected_parent.front(): NULL;
773 }
774 CDir *get_parent_dir();
775 const CDir *get_projected_parent_dir() const;
776 CDir *get_projected_parent_dir();
777 CInode *get_parent_inode();
778
779 bool is_lt(const MDSCacheObject *r) const override {
780 const CInode *o = static_cast<const CInode*>(r);
781 return ino() < o->ino() ||
782 (ino() == o->ino() && last < o->last);
783 }
784
785 // -- misc --
786 bool is_ancestor_of(const CInode *other) const;
787 bool is_projected_ancestor_of(const CInode *other) const;
788
789 void make_path_string(std::string& s, bool projected=false, const CDentry *use_parent=NULL) const;
790 void make_path(filepath& s, bool projected=false) const;
791 void name_stray_dentry(std::string& dname);
792
793 // -- dirtyness --
794 version_t get_version() const { return inode.version; }
795
796 version_t pre_dirty();
797 void _mark_dirty(LogSegment *ls);
798 void mark_dirty(version_t projected_dirv, LogSegment *ls);
799 void mark_clean();
800
801 void store(MDSContext *fin);
802 void _stored(int r, version_t cv, Context *fin);
803 /**
804 * Flush a CInode to disk. This includes the backtrace, the parent
805 * directory's link, and the Inode object itself (if a base directory).
806 * @pre is_auth() on both the inode and its containing directory
807 * @pre can_auth_pin()
808 * @param fin The Context to call when the flush is completed.
809 */
810 void flush(MDSContext *fin);
811 void fetch(MDSContext *fin);
812 void _fetched(bufferlist& bl, bufferlist& bl2, Context *fin);
813
814
815 void build_backtrace(int64_t pool, inode_backtrace_t& bt);
816 void store_backtrace(MDSContext *fin, int op_prio=-1);
817 void _stored_backtrace(int r, version_t v, Context *fin);
818 void fetch_backtrace(Context *fin, bufferlist *backtrace);
819 protected:
820 /**
821 * Return the pool ID where we currently write backtraces for
822 * this inode (in addition to inode.old_pools)
823 *
824 * @returns a pool ID >=0
825 */
826 int64_t get_backtrace_pool() const;
827 public:
828 void mark_dirty_parent(LogSegment *ls, bool dirty_pool=false);
829 void clear_dirty_parent();
830 void verify_diri_backtrace(bufferlist &bl, int err);
831 bool is_dirty_parent() { return state_test(STATE_DIRTYPARENT); }
832 bool is_dirty_pool() { return state_test(STATE_DIRTYPOOL); }
833
834 void encode_snap_blob(bufferlist &bl);
835 void decode_snap_blob(const bufferlist &bl);
836 void encode_store(bufferlist& bl, uint64_t features);
837 void decode_store(bufferlist::const_iterator& bl);
838
839 void encode_replica(mds_rank_t rep, bufferlist& bl, uint64_t features, bool need_recover) {
840 ceph_assert(is_auth());
841
842 __u32 nonce = add_replica(rep);
843 using ceph::encode;
844 encode(nonce, bl);
845
846 _encode_base(bl, features);
847 _encode_locks_state_for_replica(bl, need_recover);
848 }
849 void decode_replica(bufferlist::const_iterator& p, bool is_new) {
850 using ceph::decode;
851 __u32 nonce;
852 decode(nonce, p);
853 replica_nonce = nonce;
854
855 _decode_base(p);
856 _decode_locks_state(p, is_new);
857 }
858
859 // -- waiting --
860 protected:
861 mempool::mds_co::compact_map<frag_t, MDSContext::vec > waiting_on_dir;
862 public:
863 void add_dir_waiter(frag_t fg, MDSContext *c);
864 void take_dir_waiting(frag_t fg, MDSContext::vec& ls);
865 bool is_waiting_for_dir(frag_t fg) {
866 return waiting_on_dir.count(fg);
867 }
868 void add_waiter(uint64_t tag, MDSContext *c) override;
869 void take_waiting(uint64_t tag, MDSContext::vec& ls) override;
870
871 // -- encode/decode helpers --
872 void _encode_base(bufferlist& bl, uint64_t features);
873 void _decode_base(bufferlist::const_iterator& p);
874 void _encode_locks_full(bufferlist& bl);
875 void _decode_locks_full(bufferlist::const_iterator& p);
876 void _encode_locks_state_for_replica(bufferlist& bl, bool need_recover);
877 void _encode_locks_state_for_rejoin(bufferlist& bl, int rep);
878 void _decode_locks_state(bufferlist::const_iterator& p, bool is_new);
879 void _decode_locks_rejoin(bufferlist::const_iterator& p, MDSContext::vec& waiters,
880 std::list<SimpleLock*>& eval_locks, bool survivor);
881
882 // -- import/export --
883 void encode_export(bufferlist& bl);
884 void finish_export();
885 void abort_export() {
886 put(PIN_TEMPEXPORTING);
887 ceph_assert(state_test(STATE_EXPORTINGCAPS));
888 state_clear(STATE_EXPORTINGCAPS);
889 put(PIN_EXPORTINGCAPS);
890 }
891 void decode_import(bufferlist::const_iterator& p, LogSegment *ls);
892
893
894 // for giving to clients
895 int encode_inodestat(bufferlist& bl, Session *session, SnapRealm *realm,
896 snapid_t snapid=CEPH_NOSNAP, unsigned max_bytes=0,
897 int getattr_wants=0);
898 void encode_cap_message(const MClientCaps::ref &m, Capability *cap);
899
900
901 // -- locks --
902 public:
903 static LockType versionlock_type;
904 static LockType authlock_type;
905 static LockType linklock_type;
906 static LockType dirfragtreelock_type;
907 static LockType filelock_type;
908 static LockType xattrlock_type;
909 static LockType snaplock_type;
910 static LockType nestlock_type;
911 static LockType flocklock_type;
912 static LockType policylock_type;
913
914 // FIXME not part of mempool
915 LocalLock versionlock;
916 SimpleLock authlock;
917 SimpleLock linklock;
918 ScatterLock dirfragtreelock;
919 ScatterLock filelock;
920 SimpleLock xattrlock;
921 SimpleLock snaplock;
922 ScatterLock nestlock;
923 SimpleLock flocklock;
924 SimpleLock policylock;
925
926 SimpleLock* get_lock(int type) override {
927 switch (type) {
928 case CEPH_LOCK_IFILE: return &filelock;
929 case CEPH_LOCK_IAUTH: return &authlock;
930 case CEPH_LOCK_ILINK: return &linklock;
931 case CEPH_LOCK_IDFT: return &dirfragtreelock;
932 case CEPH_LOCK_IXATTR: return &xattrlock;
933 case CEPH_LOCK_ISNAP: return &snaplock;
934 case CEPH_LOCK_INEST: return &nestlock;
935 case CEPH_LOCK_IFLOCK: return &flocklock;
936 case CEPH_LOCK_IPOLICY: return &policylock;
937 }
938 return 0;
939 }
940
941 void set_object_info(MDSCacheObjectInfo &info) override;
942 void encode_lock_state(int type, bufferlist& bl) override;
943 void decode_lock_state(int type, const bufferlist& bl) override;
944
945 void _finish_frag_update(CDir *dir, MutationRef& mut);
946
947 void clear_dirty_scattered(int type) override;
948 bool is_dirty_scattered();
949 void clear_scatter_dirty(); // on rejoin ack
950
951 void start_scatter(ScatterLock *lock);
952 void finish_scatter_update(ScatterLock *lock, CDir *dir,
953 version_t inode_version, version_t dir_accounted_version);
954 void finish_scatter_gather_update(int type);
955 void finish_scatter_gather_update_accounted(int type, MutationRef& mut, EMetaBlob *metablob);
956
957 // -- snap --
958 void open_snaprealm(bool no_split=false);
959 void close_snaprealm(bool no_join=false);
960 SnapRealm *find_snaprealm() const;
961 void encode_snap(bufferlist& bl);
962 void decode_snap(bufferlist::const_iterator& p);
963
964 // -- caps -- (new)
965 // client caps
966 client_t loner_cap = -1, want_loner_cap = -1;
967
968 client_t get_loner() const { return loner_cap; }
969 client_t get_wanted_loner() const { return want_loner_cap; }
970
971 // this is the loner state our locks should aim for
972 client_t get_target_loner() const {
973 if (loner_cap == want_loner_cap)
974 return loner_cap;
975 else
976 return -1;
977 }
978
979 client_t calc_ideal_loner();
980 void set_loner_cap(client_t l);
981 bool choose_ideal_loner();
982 bool try_set_loner();
983 bool try_drop_loner();
984
985 // choose new lock state during recovery, based on issued caps
986 void choose_lock_state(SimpleLock *lock, int allissued);
987 void choose_lock_states(int dirty_caps);
988
989 int count_nonstale_caps() {
990 int n = 0;
991 for (const auto &p : client_caps) {
992 if (!p.second.is_stale())
993 n++;
994 }
995 return n;
996 }
997 bool multiple_nonstale_caps() {
998 int n = 0;
999 for (const auto &p : client_caps) {
1000 if (!p.second.is_stale()) {
1001 if (n)
1002 return true;
1003 n++;
1004 }
1005 }
1006 return false;
1007 }
1008
1009 bool is_any_caps() { return !client_caps.empty(); }
1010 bool is_any_nonstale_caps() { return count_nonstale_caps(); }
1011
1012 const mempool::mds_co::compact_map<int32_t,int32_t>& get_mds_caps_wanted() const { return mds_caps_wanted; }
1013 void set_mds_caps_wanted(mempool::mds_co::compact_map<int32_t,int32_t>& m);
1014 void set_mds_caps_wanted(mds_rank_t mds, int32_t wanted);
1015
1016 const mempool_cap_map& get_client_caps() const { return client_caps; }
1017 Capability *get_client_cap(client_t client) {
1018 auto client_caps_entry = client_caps.find(client);
1019 if (client_caps_entry != client_caps.end())
1020 return &client_caps_entry->second;
1021 return 0;
1022 }
1023 int get_client_cap_pending(client_t client) const {
1024 auto client_caps_entry = client_caps.find(client);
1025 if (client_caps_entry != client_caps.end()) {
1026 return client_caps_entry->second.pending();
1027 } else {
1028 return 0;
1029 }
1030 }
1031
1032 int get_num_caps_wanted() const { return num_caps_wanted; }
1033 void adjust_num_caps_wanted(int d);
1034
1035 Capability *add_client_cap(client_t client, Session *session, SnapRealm *conrealm=0);
1036 void remove_client_cap(client_t client);
1037 void move_to_realm(SnapRealm *realm);
1038
1039 Capability *reconnect_cap(client_t client, const cap_reconnect_t& icr, Session *session);
1040 void clear_client_caps_after_export();
1041 void export_client_caps(std::map<client_t,Capability::Export>& cl);
1042
1043 // caps allowed
1044 int get_caps_liked() const;
1045 int get_caps_allowed_ever() const;
1046 int get_caps_allowed_by_type(int type) const;
1047 int get_caps_careful() const;
1048 int get_xlocker_mask(client_t client) const;
1049 int get_caps_allowed_for_client(Session *s, Capability *cap, mempool_inode *file_i) const;
1050
1051 // caps issued, wanted
1052 int get_caps_issued(int *ploner = 0, int *pother = 0, int *pxlocker = 0,
1053 int shift = 0, int mask = -1);
1054 bool is_any_caps_wanted() const;
1055 int get_caps_wanted(int *ploner = 0, int *pother = 0, int shift = 0, int mask = -1) const;
1056 bool issued_caps_need_gather(SimpleLock *lock);
1057
1058 // -- authority --
1059 mds_authority_t authority() const override;
1060
1061 // -- auth pins --
1062 bool can_auth_pin(int *err_ret=nullptr) const override;
1063 void auth_pin(void *by) override;
1064 void auth_unpin(void *by) override;
1065
1066 // -- freeze --
1067 bool is_freezing_inode() const { return state_test(STATE_FREEZING); }
1068 bool is_frozen_inode() const { return state_test(STATE_FROZEN); }
1069 bool is_frozen_auth_pin() const { return state_test(STATE_FROZENAUTHPIN); }
1070 bool is_frozen() const override;
1071 bool is_frozen_dir() const;
1072 bool is_freezing() const override;
1073
1074 /* Freeze the inode. auth_pin_allowance lets the caller account for any
1075 * auth_pins it is itself holding/responsible for. */
1076 bool freeze_inode(int auth_pin_allowance=0);
1077 void unfreeze_inode(MDSContext::vec& finished);
1078 void unfreeze_inode();
1079
1080 void freeze_auth_pin();
1081 void unfreeze_auth_pin();
1082
1083 // -- reference counting --
1084 void bad_put(int by) override {
1085 generic_dout(0) << " bad put " << *this << " by " << by << " " << pin_name(by) << " was " << ref
1086 #ifdef MDS_REF_SET
1087 << " (" << ref_map << ")"
1088 #endif
1089 << dendl;
1090 #ifdef MDS_REF_SET
1091 ceph_assert(ref_map[by] > 0);
1092 #endif
1093 ceph_assert(ref > 0);
1094 }
1095 void bad_get(int by) override {
1096 generic_dout(0) << " bad get " << *this << " by " << by << " " << pin_name(by) << " was " << ref
1097 #ifdef MDS_REF_SET
1098 << " (" << ref_map << ")"
1099 #endif
1100 << dendl;
1101 #ifdef MDS_REF_SET
1102 ceph_assert(ref_map[by] >= 0);
1103 #endif
1104 }
1105 void first_get() override;
1106 void last_put() override;
1107 void _put() override;
1108
1109
1110 // -- hierarchy stuff --
1111 public:
1112 void set_primary_parent(CDentry *p) {
1113 ceph_assert(parent == 0 ||
1114 g_conf().get_val<bool>("mds_hack_allow_loading_invalid_metadata"));
1115 parent = p;
1116 }
1117 void remove_primary_parent(CDentry *dn) {
1118 ceph_assert(dn == parent);
1119 parent = 0;
1120 }
1121 void add_remote_parent(CDentry *p);
1122 void remove_remote_parent(CDentry *p);
1123 int num_remote_parents() {
1124 return remote_parents.size();
1125 }
1126
1127 void push_projected_parent(CDentry *dn) {
1128 projected_parent.push_back(dn);
1129 }
1130 void pop_projected_parent() {
1131 ceph_assert(projected_parent.size());
1132 parent = projected_parent.front();
1133 projected_parent.pop_front();
1134 }
1135
1136 public:
1137 void maybe_export_pin(bool update=false);
1138 void set_export_pin(mds_rank_t rank);
1139 mds_rank_t get_export_pin(bool inherit=true) const;
1140 bool is_exportable(mds_rank_t dest) const;
1141
1142 void print(ostream& out) override;
1143 void dump(Formatter *f, int flags = DUMP_DEFAULT) const;
1144
1145 /**
1146 * @defgroup Scrubbing and fsck
1147 * @{
1148 */
1149
1150 /**
1151 * Report the results of validation against a particular inode.
1152 * Each member is a pair of bools.
1153 * <member>.first represents if validation was performed against the member.
1154 * <member.second represents if the member passed validation.
1155 * performed_validation is set to true if the validation was actually
1156 * run. It might not be run if, for instance, the inode is marked as dirty.
1157 * passed_validation is set to true if everything that was checked
1158 * passed its validation.
1159 */
1160 struct validated_data {
1161 template<typename T>struct member_status {
1162 bool checked = false;
1163 bool passed = false;
1164 bool repaired = false;
1165 int ondisk_read_retval = 0;
1166 T ondisk_value;
1167 T memory_value;
1168 std::stringstream error_str;
1169 };
1170
1171 bool performed_validation = false;
1172 bool passed_validation = false;
1173
1174 struct raw_stats_t {
1175 frag_info_t dirstat;
1176 nest_info_t rstat;
1177 };
1178
1179 member_status<inode_backtrace_t> backtrace;
1180 member_status<mempool_inode> inode; // XXX should not be in mempool; wait for pmr
1181 member_status<raw_stats_t> raw_stats;
1182
1183 validated_data() {}
1184
1185 void dump(Formatter *f) const;
1186
1187 bool all_damage_repaired() const;
1188 };
1189
1190 /**
1191 * Validate that the on-disk state of an inode matches what
1192 * we expect from our memory state. Currently this checks that:
1193 * 1) The backtrace associated with the file data exists and is correct
1194 * 2) For directories, the actual inode metadata matches our memory state,
1195 * 3) For directories, the rstats match
1196 *
1197 * @param results A freshly-created validated_data struct, with values set
1198 * as described in the struct documentation.
1199 * @param mdr The request to be responeded upon the completion of the
1200 * validation (or NULL)
1201 * @param fin Context to call back on completion (or NULL)
1202 */
1203 void validate_disk_state(validated_data *results,
1204 MDSContext *fin);
1205 static void dump_validation_results(const validated_data& results,
1206 Formatter *f);
1207 private:
1208 bool _validate_disk_state(class ValidationContinuation *c,
1209 int rval, int stage);
1210 friend class ValidationContinuation;
1211 /** @} Scrubbing and fsck */
1212 };
1213
1214 ostream& operator<<(ostream& out, const CInode::scrub_stamp_info_t& si);
1215
1216 #undef dout_context
1217 #endif