]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- | |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #ifndef CEPH_CLIENT_INODE_H | |
5 | #define CEPH_CLIENT_INODE_H | |
6 | ||
7 | #include <numeric> | |
8 | ||
9 | #include "include/compat.h" | |
10 | #include "include/ceph_assert.h" | |
11 | #include "include/types.h" | |
12 | #include "include/xlist.h" | |
13 | ||
14 | #include "mds/flock.h" | |
15 | #include "mds/mdstypes.h" // hrm | |
16 | ||
17 | #include "osdc/ObjectCacher.h" | |
18 | ||
19 | #include "InodeRef.h" | |
20 | #include "MetaSession.h" | |
21 | #include "UserPerm.h" | |
22 | #include "Delegation.h" | |
23 | ||
24 | class Client; | |
25 | class Dentry; | |
26 | class Dir; | |
27 | struct SnapRealm; | |
28 | struct Inode; | |
29 | class MetaRequest; | |
30 | class filepath; | |
31 | class Fh; | |
32 | ||
33 | class Cap { | |
34 | public: | |
35 | Cap() = delete; | |
36 | Cap(Inode &i, MetaSession *s) : inode(i), | |
37 | session(s), | |
38 | gen(s->cap_gen), | |
39 | cap_item(this) | |
40 | { | |
41 | s->caps.push_back(&cap_item); | |
42 | } | |
43 | ~Cap() { | |
44 | cap_item.remove_myself(); | |
45 | } | |
46 | ||
47 | void touch(void) { | |
48 | // move to back of LRU | |
49 | session->caps.push_back(&cap_item); | |
50 | } | |
51 | ||
52 | void dump(Formatter *f) const; | |
53 | ||
54 | Inode &inode; | |
55 | MetaSession *session; | |
56 | uint64_t cap_id = 0; | |
57 | unsigned issued = 0; | |
58 | unsigned implemented = 0; | |
59 | unsigned wanted = 0; // as known to mds. | |
60 | uint64_t seq = 0; | |
61 | uint64_t issue_seq = 0; | |
62 | __u32 mseq = 0; // migration seq | |
63 | __u32 gen; | |
64 | UserPerm latest_perms; | |
65 | ||
66 | private: | |
67 | /* Note that this Cap will not move (see Inode::caps): | |
68 | * | |
69 | * Section 23.1.2#8 | |
70 | * The insert members shall not affect the validity of iterators and | |
71 | * references to the container, and the erase members shall invalidate only | |
72 | * iterators and references to the erased elements. | |
73 | */ | |
74 | xlist<Cap *>::item cap_item; | |
75 | }; | |
76 | ||
77 | struct CapSnap { | |
78 | //snapid_t follows; // map key | |
79 | InodeRef in; | |
80 | SnapContext context; | |
81 | int issued = 0, dirty = 0; | |
82 | ||
83 | uint64_t size = 0; | |
84 | utime_t ctime, btime, mtime, atime; | |
85 | version_t time_warp_seq = 0; | |
86 | uint64_t change_attr = 0; | |
87 | uint32_t mode = 0; | |
88 | uid_t uid = 0; | |
89 | gid_t gid = 0; | |
90 | map<string,bufferptr> xattrs; | |
91 | version_t xattr_version = 0; | |
92 | ||
93 | bufferlist inline_data; | |
94 | version_t inline_version = 0; | |
95 | ||
96 | bool writing = false, dirty_data = false; | |
97 | uint64_t flush_tid = 0; | |
98 | ||
99 | int64_t cap_dirtier_uid = -1; | |
100 | int64_t cap_dirtier_gid = -1; | |
101 | ||
102 | explicit CapSnap(Inode *i) | |
103 | : in(i) | |
104 | {} | |
105 | ||
106 | void dump(Formatter *f) const; | |
107 | }; | |
108 | ||
109 | // inode flags | |
110 | #define I_COMPLETE (1 << 0) | |
111 | #define I_DIR_ORDERED (1 << 1) | |
112 | #define I_SNAPDIR_OPEN (1 << 2) | |
113 | #define I_KICK_FLUSH (1 << 3) | |
114 | #define I_CAP_DROPPED (1 << 4) | |
115 | #define I_ERROR_FILELOCK (1 << 5) | |
116 | ||
117 | struct Inode : RefCountedObject { | |
118 | Client *client; | |
119 | ||
120 | // -- the actual inode -- | |
121 | inodeno_t ino; // ORDER DEPENDENCY: oset | |
122 | snapid_t snapid; | |
123 | ino_t faked_ino = 0; | |
124 | ||
125 | uint32_t rdev = 0; // if special file | |
126 | ||
127 | // affected by any inode change... | |
128 | utime_t ctime; // inode change time | |
129 | utime_t btime; // birth time | |
130 | ||
131 | // perm (namespace permissions) | |
132 | uint32_t mode = 0; | |
133 | uid_t uid = 0; | |
134 | gid_t gid = 0; | |
135 | ||
136 | // nlink | |
137 | int32_t nlink = 0; | |
138 | ||
139 | // file (data access) | |
140 | ceph_dir_layout dir_layout{}; | |
141 | file_layout_t layout; | |
142 | uint64_t size = 0; // on directory, # dentries | |
143 | uint32_t truncate_seq = 1; | |
144 | uint64_t truncate_size = -1; | |
145 | utime_t mtime; // file data modify time. | |
146 | utime_t atime; // file data access time. | |
147 | uint32_t time_warp_seq = 0; // count of (potential) mtime/atime timewarps (i.e., utimes()) | |
148 | uint64_t change_attr = 0; | |
149 | ||
150 | uint64_t max_size = 0; // max size we can write to | |
151 | ||
152 | // dirfrag, recursive accountin | |
153 | frag_info_t dirstat; | |
154 | nest_info_t rstat; | |
155 | ||
156 | // special stuff | |
157 | version_t version = 0; // auth only | |
158 | version_t xattr_version = 0; | |
159 | utime_t snap_btime; // snapshot creation (birth) time | |
160 | std::map<std::string, std::string> snap_metadata; | |
161 | ||
162 | // inline data | |
163 | version_t inline_version = 0; | |
164 | bufferlist inline_data; | |
165 | ||
166 | bool fscrypt = false; // fscrypt enabled ? | |
167 | ||
168 | bool is_root() const { return ino == CEPH_INO_ROOT; } | |
169 | bool is_symlink() const { return (mode & S_IFMT) == S_IFLNK; } | |
170 | bool is_dir() const { return (mode & S_IFMT) == S_IFDIR; } | |
171 | bool is_file() const { return (mode & S_IFMT) == S_IFREG; } | |
172 | ||
173 | bool has_dir_layout() const { | |
174 | return layout != file_layout_t(); | |
175 | } | |
176 | ||
177 | __u32 hash_dentry_name(const string &dn) { | |
178 | int which = dir_layout.dl_dir_hash; | |
179 | if (!which) | |
180 | which = CEPH_STR_HASH_LINUX; | |
181 | ceph_assert(ceph_str_hash_valid(which)); | |
182 | return ceph_str_hash(which, dn.data(), dn.length()); | |
183 | } | |
184 | ||
185 | unsigned flags = 0; | |
186 | ||
187 | quota_info_t quota; | |
188 | ||
189 | bool is_complete_and_ordered() { | |
190 | static const unsigned wants = I_COMPLETE | I_DIR_ORDERED; | |
191 | return (flags & wants) == wants; | |
192 | } | |
193 | ||
194 | // about the dir (if this is one!) | |
195 | Dir *dir = 0; // if i'm a dir. | |
196 | fragtree_t dirfragtree; | |
197 | uint64_t dir_release_count = 1; | |
198 | uint64_t dir_ordered_count = 1; | |
199 | bool dir_hashed = false; | |
200 | bool dir_replicated = false; | |
201 | ||
202 | // per-mds caps | |
203 | std::map<mds_rank_t, Cap> caps; // mds -> Cap | |
204 | Cap *auth_cap = 0; | |
205 | int64_t cap_dirtier_uid = -1; | |
206 | int64_t cap_dirtier_gid = -1; | |
207 | unsigned dirty_caps = 0; | |
208 | unsigned flushing_caps = 0; | |
209 | std::map<ceph_tid_t, int> flushing_cap_tids; | |
210 | int shared_gen = 0; | |
211 | int cache_gen = 0; | |
212 | int snap_caps = 0; | |
213 | int snap_cap_refs = 0; | |
214 | utime_t hold_caps_until; | |
215 | xlist<Inode*>::item delay_cap_item, dirty_cap_item, flushing_cap_item; | |
216 | ||
217 | SnapRealm *snaprealm = 0; | |
218 | xlist<Inode*>::item snaprealm_item; | |
219 | InodeRef snapdir_parent; // only if we are a snapdir inode | |
220 | map<snapid_t,CapSnap> cap_snaps; // pending flush to mds | |
221 | ||
222 | //int open_by_mode[CEPH_FILE_MODE_NUM]; | |
223 | map<int,int> open_by_mode; | |
224 | map<int,int> cap_refs; | |
225 | ||
226 | ObjectCacher::ObjectSet oset; // ORDER DEPENDENCY: ino | |
227 | ||
228 | uint64_t reported_size = 0; | |
229 | uint64_t wanted_max_size = 0; | |
230 | uint64_t requested_max_size = 0; | |
231 | ||
232 | uint64_t ll_ref = 0; // separate ref count for ll client | |
233 | xlist<Dentry *> dentries; // if i'm linked to a dentry. | |
234 | string symlink; // symlink content, if it's a symlink | |
235 | map<string,bufferptr> xattrs; | |
236 | map<frag_t,int> fragmap; // known frag -> mds mappings | |
237 | map<frag_t, std::vector<mds_rank_t>> frag_repmap; // non-auth mds mappings | |
238 | ||
239 | std::list<ceph::condition_variable*> waitfor_caps; | |
240 | std::list<ceph::condition_variable*> waitfor_commit; | |
241 | std::list<ceph::condition_variable*> waitfor_deleg; | |
242 | ||
243 | Dentry *get_first_parent() { | |
244 | ceph_assert(!dentries.empty()); | |
245 | return *dentries.begin(); | |
246 | } | |
247 | ||
248 | void make_long_path(filepath& p); | |
249 | void make_short_path(filepath& p); | |
250 | void make_nosnap_relative_path(filepath& p); | |
251 | ||
252 | // The ref count. 1 for each dentry, fh, inode_map, | |
253 | // cwd that links to me. | |
254 | void iget() { get(); } | |
255 | void iput(int n=1) { ceph_assert(n >= 0); while (n--) put(); } | |
256 | ||
257 | void ll_get() { | |
258 | ll_ref++; | |
259 | } | |
260 | void ll_put(uint64_t n=1) { | |
261 | ceph_assert(ll_ref >= n); | |
262 | ll_ref -= n; | |
263 | } | |
264 | ||
265 | // file locks | |
266 | std::unique_ptr<ceph_lock_state_t> fcntl_locks; | |
267 | std::unique_ptr<ceph_lock_state_t> flock_locks; | |
268 | ||
269 | bool has_any_filelocks() { | |
270 | return | |
271 | (fcntl_locks && !fcntl_locks->empty()) || | |
272 | (flock_locks && !flock_locks->empty()); | |
273 | } | |
274 | ||
275 | list<Delegation> delegations; | |
276 | ||
277 | xlist<MetaRequest*> unsafe_ops; | |
278 | ||
279 | std::set<Fh*> fhs; | |
280 | ||
281 | mds_rank_t dir_pin = MDS_RANK_NONE; | |
282 | ||
283 | Inode() = delete; | |
284 | Inode(Client *c, vinodeno_t vino, file_layout_t *newlayout) | |
285 | : client(c), ino(vino.ino), snapid(vino.snapid), delay_cap_item(this), | |
286 | dirty_cap_item(this), flushing_cap_item(this), snaprealm_item(this), | |
287 | oset((void *)this, newlayout->pool_id, this->ino) {} | |
288 | ~Inode(); | |
289 | ||
290 | vinodeno_t vino() const { return vinodeno_t(ino, snapid); } | |
291 | ||
292 | struct Compare { | |
293 | bool operator() (Inode* const & left, Inode* const & right) { | |
294 | if (left->ino.val < right->ino.val) { | |
295 | return (left->snapid.val < right->snapid.val); | |
296 | } | |
297 | return false; | |
298 | } | |
299 | }; | |
300 | ||
301 | bool check_mode(const UserPerm& perms, unsigned want); | |
302 | ||
303 | // CAPS -------- | |
304 | void get_open_ref(int mode); | |
305 | bool put_open_ref(int mode); | |
306 | ||
307 | void get_cap_ref(int cap); | |
308 | int put_cap_ref(int cap); | |
309 | bool is_any_caps(); | |
310 | bool cap_is_valid(const Cap &cap) const; | |
311 | int caps_issued(int *implemented = 0) const; | |
312 | void try_touch_cap(mds_rank_t mds); | |
313 | bool caps_issued_mask(unsigned mask, bool allow_impl=false); | |
314 | int caps_used(); | |
315 | int caps_file_wanted(); | |
316 | int caps_wanted(); | |
317 | int caps_mds_wanted(); | |
318 | int caps_dirty(); | |
319 | const UserPerm *get_best_perms(); | |
320 | ||
321 | bool have_valid_size(); | |
322 | Dir *open_dir(); | |
323 | ||
324 | void add_fh(Fh *f) {fhs.insert(f);} | |
325 | void rm_fh(Fh *f) {fhs.erase(f);} | |
326 | void set_async_err(int r); | |
327 | void dump(Formatter *f) const; | |
328 | ||
329 | void break_all_delegs() { break_deleg(false); }; | |
330 | ||
331 | void recall_deleg(bool skip_read); | |
332 | bool has_recalled_deleg(); | |
333 | int set_deleg(Fh *fh, unsigned type, ceph_deleg_cb_t cb, void *priv); | |
334 | void unset_deleg(Fh *fh); | |
335 | ||
336 | void mark_caps_dirty(int caps); | |
337 | void mark_caps_clean(); | |
338 | private: | |
339 | // how many opens for write on this Inode? | |
340 | long open_count_for_write() | |
341 | { | |
342 | return (long)(open_by_mode[CEPH_FILE_MODE_RDWR] + | |
343 | open_by_mode[CEPH_FILE_MODE_WR]); | |
344 | }; | |
345 | ||
346 | // how many opens of any sort on this inode? | |
347 | long open_count() | |
348 | { | |
349 | return (long) std::accumulate(open_by_mode.begin(), open_by_mode.end(), 0, | |
350 | [] (int value, const std::map<int, int>::value_type& p) | |
351 | { return value + p.second; }); | |
352 | }; | |
353 | ||
354 | void break_deleg(bool skip_read); | |
355 | bool delegations_broken(bool skip_read); | |
356 | ||
357 | }; | |
358 | ||
359 | ostream& operator<<(ostream &out, const Inode &in); | |
360 | ||
361 | #endif |