1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
15 #ifndef CEPH_MDS_SNAPREALM_H
16 #define CEPH_MDS_SNAPREALM_H
20 #include "include/xlist.h"
21 #include "include/elist.h"
22 #include "common/snap_types.h"
24 class MDSInternalContextBase;
29 mutable snapid_t cached_seq; // max seq over self and all past+present parents.
30 mutable snapid_t cached_last_created; // max last_created over all past+present parents
31 mutable snapid_t cached_last_destroyed;
32 mutable set<snapid_t> cached_snaps;
33 mutable SnapContext cached_snap_context;
34 mutable bufferlist cached_snap_trace;
36 void check_cache() const;
46 bool open; // set to true once all past_parents are opened
48 set<SnapRealm*> open_children; // active children that are currently open
49 set<SnapRealm*> open_past_children; // past children who has pinned me
50 map<inodeno_t, pair<SnapRealm*, set<snapid_t> > > open_past_parents; // these are explicitly pinned.
51 unsigned num_open_past_parents;
55 elist<CInode*> inodes_with_caps; // for efficient realm splits
56 map<client_t, xlist<Capability*>* > client_caps; // to identify clients who need snap notifications
58 SnapRealm(MDCache *c, CInode *in) :
60 mdcache(c), inode(in),
61 open(false), parent(0),
62 num_open_past_parents(0),
66 bool exists(const string &name) const {
67 for (map<snapid_t,SnapInfo>::const_iterator p = srnode.snaps.begin();
68 p != srnode.snaps.end();
70 if (p->second.name == name)
76 bool is_open() const { return open; }
77 void _close_parents() { open = false; }
78 bool _open_parents(MDSInternalContextBase *retryorfinish, snapid_t first=1, snapid_t last=CEPH_NOSNAP);
79 bool open_parents(MDSInternalContextBase *retryorfinish);
80 void _remove_missing_parent(snapid_t snapid, inodeno_t parent, int err);
81 bool have_past_parents_open(snapid_t first=1, snapid_t last=CEPH_NOSNAP);
82 void add_open_past_parent(SnapRealm *parent, snapid_t last);
83 void remove_open_past_parent(inodeno_t ino, snapid_t last);
86 void prune_past_parents();
87 bool has_past_parents() const { return !srnode.past_parents.empty(); }
89 void build_snap_set(set<snapid_t>& s,
90 snapid_t& max_seq, snapid_t& max_last_created, snapid_t& max_last_destroyed,
91 snapid_t first, snapid_t last) const;
92 void get_snap_info(map<snapid_t,SnapInfo*>& infomap, snapid_t first=0, snapid_t last=CEPH_NOSNAP);
94 const bufferlist& get_snap_trace();
95 void build_snap_trace(bufferlist& snapbl) const;
97 const string& get_snapname(snapid_t snapid, inodeno_t atino);
98 snapid_t resolve_snapname(const string &name, inodeno_t atino, snapid_t first=0, snapid_t last=CEPH_NOSNAP);
100 const set<snapid_t>& get_snaps() const;
101 const SnapContext& get_snap_context() const;
102 void invalidate_cached_snaps() {
105 snapid_t get_last_created() {
107 return cached_last_created;
109 snapid_t get_last_destroyed() {
111 return cached_last_destroyed;
113 snapid_t get_newest_snap() {
115 if (cached_snaps.empty())
118 return *cached_snaps.rbegin();
120 snapid_t get_newest_seq() {
125 snapid_t get_snap_following(snapid_t follows) {
127 const set<snapid_t>& s = get_snaps();
128 set<snapid_t>::const_iterator p = s.upper_bound(follows);
134 bool has_snaps_in_range(snapid_t first, snapid_t last) {
136 const set<snapid_t>& s = get_snaps();
137 set<snapid_t>::const_iterator p = s.lower_bound(first);
138 return (p != s.end() && *p <= last);
141 void adjust_parent();
143 void split_at(SnapRealm *child);
144 void join(SnapRealm *child);
146 void add_cap(client_t client, Capability *cap) {
147 auto client_caps_entry = client_caps.find(client);
148 if (client_caps_entry == client_caps.end())
149 client_caps_entry = client_caps.emplace(client,
150 new xlist<Capability*>).first;
151 client_caps_entry->second->push_back(&cap->item_snaprealm_caps);
153 void remove_cap(client_t client, Capability *cap) {
154 cap->item_snaprealm_caps.remove_myself();
155 if (client_caps[client]->empty()) {
156 delete client_caps[client];
157 client_caps.erase(client);
163 ostream& operator<<(ostream& out, const SnapRealm &realm);