1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
9 #include "MetaSession.h"
10 #include "ClientSnapRealm.h"
11 #include "Delegation.h"
13 #include "mds/flock.h"
23 delay_cap_item
.remove_myself();
24 dirty_cap_item
.remove_myself();
25 snaprealm_item
.remove_myself();
28 snapdir_parent
->flags
&= ~I_SNAPDIR_OPEN
;
29 snapdir_parent
.reset();
32 if (!oset
.objects
.empty()) {
33 lsubdout(client
->cct
, client
, 0) << __func__
<< ": leftover objects on inode 0x"
34 << std::hex
<< ino
<< std::dec
<< dendl
;
35 ceph_assert(oset
.objects
.empty());
38 if (!delegations
.empty()) {
39 lsubdout(client
->cct
, client
, 0) << __func__
<< ": leftover delegations on inode 0x"
40 << std::hex
<< ino
<< std::dec
<< dendl
;
41 ceph_assert(delegations
.empty());
45 ostream
& operator<<(ostream
&out
, const Inode
&in
)
47 out
<< in
.vino() << "("
48 << "faked_ino=" << in
.faked_ino
49 << " nref=" << in
.get_nref()
50 << " ll_ref=" << in
.ll_ref
51 << " cap_refs=" << in
.cap_refs
52 << " open=" << in
.open_by_mode
53 << " mode=" << oct
<< in
.mode
<< dec
54 << " size=" << in
.size
<< "/" << in
.max_size
55 << " nlink=" << in
.nlink
56 << " btime=" << in
.btime
57 << " mtime=" << in
.mtime
58 << " ctime=" << in
.ctime
59 << " caps=" << ccap_string(in
.caps_issued());
60 if (!in
.caps
.empty()) {
63 for (const auto &pair
: in
.caps
) {
66 out
<< pair
.first
<< '=' << ccap_string(pair
.second
.issued
);
72 out
<< " dirty_caps=" << ccap_string(in
.dirty_caps
);
74 out
<< " flushing_caps=" << ccap_string(in
.flushing_caps
);
76 if (in
.flags
& I_COMPLETE
)
80 out
<< " " << in
.oset
;
82 if (!in
.dentries
.empty())
83 out
<< " parents=" << in
.dentries
;
85 if (in
.is_dir() && in
.has_dir_layout())
86 out
<< " has_dir_layout";
88 if (in
.quota
.is_enable())
89 out
<< " " << in
.quota
;
91 out
<< ' ' << &in
<< ")";
96 void Inode::make_long_path(filepath
& p
)
98 if (!dentries
.empty()) {
99 Dentry
*dn
= get_first_parent();
100 ceph_assert(dn
->dir
&& dn
->dir
->parent_inode
);
101 dn
->dir
->parent_inode
->make_long_path(p
);
102 p
.push_dentry(dn
->name
);
103 } else if (snapdir_parent
) {
104 make_nosnap_relative_path(p
);
109 void Inode::make_short_path(filepath
& p
)
111 if (!dentries
.empty()) {
112 Dentry
*dn
= get_first_parent();
113 ceph_assert(dn
->dir
&& dn
->dir
->parent_inode
);
114 p
= filepath(dn
->name
, dn
->dir
->parent_inode
->ino
);
115 } else if (snapdir_parent
) {
116 make_nosnap_relative_path(p
);
122 * make a filepath suitable for an mds request:
123 * - if we are non-snapped/live, the ino is sufficient, e.g. #1234
124 * - if we are snapped, make filepath relative to first non-snapped parent.
126 void Inode::make_nosnap_relative_path(filepath
& p
)
128 if (snapid
== CEPH_NOSNAP
) {
130 } else if (snapdir_parent
) {
131 snapdir_parent
->make_nosnap_relative_path(p
);
133 p
.push_dentry(empty
);
134 } else if (!dentries
.empty()) {
135 Dentry
*dn
= get_first_parent();
136 ceph_assert(dn
->dir
&& dn
->dir
->parent_inode
);
137 dn
->dir
->parent_inode
->make_nosnap_relative_path(p
);
138 p
.push_dentry(dn
->name
);
144 void Inode::get_open_ref(int mode
)
146 client
->inc_opened_files();
147 if (open_by_mode
[mode
] == 0) {
148 client
->inc_opened_inodes();
150 open_by_mode
[mode
]++;
151 break_deleg(!(mode
& CEPH_FILE_MODE_WR
));
154 bool Inode::put_open_ref(int mode
)
156 //cout << "open_by_mode[" << mode << "] " << open_by_mode[mode] << " -> " << (open_by_mode[mode]-1) << std::endl;
157 auto& ref
= open_by_mode
.at(mode
);
158 ceph_assert(ref
> 0);
159 client
->dec_opened_files();
161 client
->dec_opened_inodes();
167 void Inode::get_cap_ref(int cap
)
174 //cout << "inode " << *this << " get " << cap_string(c) << " " << (cap_refs[c]-1) << " -> " << cap_refs[c] << std::endl;
181 int Inode::put_cap_ref(int cap
)
188 if (cap_refs
[c
] <= 0) {
189 lderr(client
->cct
) << "put_cap_ref " << ccap_string(c
) << " went negative on " << *this << dendl
;
190 ceph_assert(cap_refs
[c
] > 0);
192 if (--cap_refs
[c
] == 0)
194 //cout << "inode " << *this << " put " << cap_string(c) << " " << (cap_refs[c]+1) << " -> " << cap_refs[c] << std::endl;
202 bool Inode::is_any_caps()
204 return !caps
.empty() || snap_caps
;
207 bool Inode::cap_is_valid(const Cap
&cap
) const
209 /*cout << "cap_gen " << cap->session-> cap_gen << std::endl
210 << "session gen " << cap->gen << std::endl
211 << "cap expire " << cap->session->cap_ttl << std::endl
212 << "cur time " << ceph_clock_now(cct) << std::endl;*/
213 if ((cap
.session
->cap_gen
<= cap
.gen
)
214 && (ceph_clock_now() < cap
.session
->cap_ttl
)) {
220 int Inode::caps_issued(int *implemented
) const
224 for (const auto &[mds
, cap
] : caps
) {
225 if (cap_is_valid(cap
)) {
227 i
|= cap
.implemented
;
230 // exclude caps issued by non-auth MDS, but are been revoking by
231 // the auth MDS. The non-auth MDS should be revoking/exporting
232 // these caps, but the message is delayed.
234 c
&= ~auth_cap
->implemented
| auth_cap
->issued
;
241 void Inode::try_touch_cap(mds_rank_t mds
)
243 auto it
= caps
.find(mds
);
244 if (it
!= caps
.end()) {
250 * caps_issued_mask - check whether we have all of the caps in the mask
251 * @mask: mask to check against
252 * @allow_impl: whether the caller can also use caps that are implemented but not issued
254 * This is the bog standard "check whether we have the required caps" operation.
255 * Typically, we only check against the capset that is currently "issued".
256 * In other words, we ignore caps that have been revoked but not yet released.
257 * Also account capability hit/miss stats.
259 * Some callers (particularly those doing attribute retrieval) can also make
260 * use of the full set of "implemented" caps to satisfy requests from the
263 * Those callers should refrain from taking new references to implemented
266 bool Inode::caps_issued_mask(unsigned mask
, bool allow_impl
)
271 if ((c
& mask
) == mask
)
275 cap_is_valid(*auth_cap
) &&
276 (auth_cap
->issued
& mask
) == mask
) {
282 for (auto &pair
: caps
) {
283 Cap
&cap
= pair
.second
;
284 if (cap_is_valid(cap
)) {
285 if ((cap
.issued
& mask
) == mask
) {
291 i
|= cap
.implemented
;
298 if ((c
& mask
) == mask
) {
299 // bah.. touch them all
300 for (auto &pair
: caps
) {
311 int Inode::caps_used()
314 for (const auto &[cap
, cnt
] : cap_refs
)
320 int Inode::caps_file_wanted()
323 for (const auto &[mode
, cnt
] : open_by_mode
)
325 want
|= ceph_caps_for_mode(mode
);
329 int Inode::caps_wanted()
331 int want
= caps_file_wanted() | caps_used();
332 if (want
& CEPH_CAP_FILE_BUFFER
)
333 want
|= CEPH_CAP_FILE_EXCL
;
337 int Inode::caps_mds_wanted()
340 for (const auto &pair
: caps
) {
341 want
|= pair
.second
.wanted
;
346 int Inode::caps_dirty()
348 return dirty_caps
| flushing_caps
;
351 const UserPerm
* Inode::get_best_perms()
353 const UserPerm
*perms
= NULL
;
354 for (const auto &pair
: caps
) {
355 const UserPerm
& iperm
= pair
.second
.latest_perms
;
356 if (!perms
) { // we don't have any, take what's present
358 } else if (iperm
.uid() == uid
) {
359 if (iperm
.gid() == gid
) { // we have the best possible, return
362 if (perms
->uid() != uid
) { // take uid > gid every time
365 } else if (perms
->uid() != uid
&& iperm
.gid() == gid
) {
366 perms
= &iperm
; // a matching gid is better than nothing
372 bool Inode::have_valid_size()
374 // RD+RDCACHE or WR+WRBUFFER => valid size
375 if (caps_issued() & (CEPH_CAP_FILE_SHARED
| CEPH_CAP_FILE_EXCL
))
380 // open Dir for an inode. if it's not open, allocated it (and pin dentry in memory).
381 Dir
*Inode::open_dir()
385 lsubdout(client
->cct
, client
, 15) << "open_dir " << dir
<< " on " << this << dendl
;
386 ceph_assert(dentries
.size() < 2); // dirs can't be hard-linked
387 if (!dentries
.empty())
388 get_first_parent()->get(); // pin dentry
394 bool Inode::check_mode(const UserPerm
& perms
, unsigned want
)
396 if (uid
== perms
.uid()) {
397 // if uid is owner, owner entry determines access
399 } else if (perms
.gid_in_groups(gid
)) {
400 // if a gid or sgid matches the owning group, group entry determines access
404 return (mode
& want
) == want
;
407 void Inode::dump(Formatter
*f
) const
409 f
->dump_stream("ino") << ino
;
410 f
->dump_stream("snapid") << snapid
;
412 f
->dump_unsigned("rdev", rdev
);
413 f
->dump_stream("ctime") << ctime
;
414 f
->dump_stream("btime") << btime
;
415 f
->dump_stream("mode") << '0' << std::oct
<< mode
<< std::dec
;
416 f
->dump_unsigned("uid", uid
);
417 f
->dump_unsigned("gid", gid
);
418 f
->dump_int("nlink", nlink
);
420 f
->dump_unsigned("size", size
);
421 f
->dump_unsigned("max_size", max_size
);
422 f
->dump_unsigned("truncate_seq", truncate_seq
);
423 f
->dump_unsigned("truncate_size", truncate_size
);
424 f
->dump_stream("mtime") << mtime
;
425 f
->dump_stream("atime") << atime
;
426 f
->dump_unsigned("time_warp_seq", time_warp_seq
);
427 f
->dump_unsigned("change_attr", change_attr
);
429 f
->dump_object("layout", layout
);
431 f
->open_object_section("dir_layout");
432 ::dump(dir_layout
, f
);
435 f
->dump_bool("complete", flags
& I_COMPLETE
);
436 f
->dump_bool("ordered", flags
& I_DIR_ORDERED
);
438 /* FIXME when wip-mds-encoding is merged ***
439 f->open_object_section("dir_stat");
443 f->open_object_section("rstat");
449 f
->dump_unsigned("version", version
);
450 f
->dump_unsigned("xattr_version", xattr_version
);
451 f
->dump_unsigned("flags", flags
);
454 f
->dump_int("dir_hashed", (int)dir_hashed
);
455 f
->dump_int("dir_replicated", (int)dir_replicated
);
456 if (dir_replicated
) {
457 f
->open_array_section("dirfrags");
458 for (const auto &frag
: frag_repmap
) {
459 f
->open_object_section("frags");
460 CachedStackStringStream css
;
461 *css
<< std::hex
<< frag
.first
.value() << "/" << std::dec
<< frag
.first
.bits();
462 f
->dump_string("frag", css
->strv());
464 f
->open_array_section("repmap");
465 for (const auto &mds
: frag
.second
) {
466 f
->dump_int("mds", mds
);
476 f
->open_array_section("caps");
477 for (const auto &pair
: caps
) {
478 f
->open_object_section("cap");
479 if (&pair
.second
== auth_cap
)
480 f
->dump_int("auth", 1);
486 f
->dump_int("auth_cap", auth_cap
->session
->mds_num
);
488 f
->dump_stream("dirty_caps") << ccap_string(dirty_caps
);
490 f
->dump_stream("flushings_caps") << ccap_string(flushing_caps
);
491 f
->open_object_section("flushing_cap_tid");
492 for (map
<ceph_tid_t
, int>::const_iterator p
= flushing_cap_tids
.begin();
493 p
!= flushing_cap_tids
.end();
495 string
n(ccap_string(p
->second
));
496 f
->dump_unsigned(n
.c_str(), p
->first
);
500 f
->dump_int("shared_gen", shared_gen
);
501 f
->dump_int("cache_gen", cache_gen
);
503 f
->dump_int("snap_caps", snap_caps
);
504 f
->dump_int("snap_cap_refs", snap_cap_refs
);
507 f
->dump_stream("hold_caps_until") << hold_caps_until
;
510 f
->open_object_section("snaprealm");
514 if (!cap_snaps
.empty()) {
515 for (const auto &p
: cap_snaps
) {
516 f
->open_object_section("cap_snap");
517 f
->dump_stream("follows") << p
.first
;
524 if (!open_by_mode
.empty()) {
525 f
->open_array_section("open_by_mode");
526 for (map
<int,int>::const_iterator p
= open_by_mode
.begin(); p
!= open_by_mode
.end(); ++p
) {
527 f
->open_object_section("ref");
528 f
->dump_int("mode", p
->first
);
529 f
->dump_int("refs", p
->second
);
534 if (!cap_refs
.empty()) {
535 f
->open_array_section("cap_refs");
536 for (map
<int,int>::const_iterator p
= cap_refs
.begin(); p
!= cap_refs
.end(); ++p
) {
537 f
->open_object_section("cap_ref");
538 f
->dump_stream("cap") << ccap_string(p
->first
);
539 f
->dump_int("refs", p
->second
);
545 f
->dump_unsigned("reported_size", reported_size
);
546 if (wanted_max_size
!= max_size
)
547 f
->dump_unsigned("wanted_max_size", wanted_max_size
);
548 if (requested_max_size
!= max_size
)
549 f
->dump_unsigned("requested_max_size", requested_max_size
);
551 f
->dump_int("nref", get_nref());
552 f
->dump_int("ll_ref", ll_ref
);
554 if (!dentries
.empty()) {
555 f
->open_array_section("parents");
556 for (const auto &&dn
: dentries
) {
557 f
->open_object_section("dentry");
558 f
->dump_stream("dir_ino") << dn
->dir
->parent_inode
->ino
;
559 f
->dump_string("name", dn
->name
);
566 void Cap::dump(Formatter
*f
) const
568 f
->dump_int("mds", session
->mds_num
);
569 f
->dump_stream("ino") << inode
.ino
;
570 f
->dump_unsigned("cap_id", cap_id
);
571 f
->dump_stream("issued") << ccap_string(issued
);
572 if (implemented
!= issued
)
573 f
->dump_stream("implemented") << ccap_string(implemented
);
574 f
->dump_stream("wanted") << ccap_string(wanted
);
575 f
->dump_unsigned("seq", seq
);
576 f
->dump_unsigned("issue_seq", issue_seq
);
577 f
->dump_unsigned("mseq", mseq
);
578 f
->dump_unsigned("gen", gen
);
581 void CapSnap::dump(Formatter
*f
) const
583 f
->dump_stream("ino") << in
->ino
;
584 f
->dump_stream("issued") << ccap_string(issued
);
585 f
->dump_stream("dirty") << ccap_string(dirty
);
586 f
->dump_unsigned("size", size
);
587 f
->dump_stream("ctime") << ctime
;
588 f
->dump_stream("mtime") << mtime
;
589 f
->dump_stream("atime") << atime
;
590 f
->dump_int("time_warp_seq", time_warp_seq
);
591 f
->dump_stream("mode") << '0' << std::oct
<< mode
<< std::dec
;
592 f
->dump_unsigned("uid", uid
);
593 f
->dump_unsigned("gid", gid
);
594 if (!xattrs
.empty()) {
595 f
->open_object_section("xattr_lens");
596 for (map
<string
,bufferptr
>::const_iterator p
= xattrs
.begin(); p
!= xattrs
.end(); ++p
)
597 f
->dump_int(p
->first
.c_str(), p
->second
.length());
600 f
->dump_unsigned("xattr_version", xattr_version
);
601 f
->dump_int("writing", (int)writing
);
602 f
->dump_int("dirty_data", (int)dirty_data
);
603 f
->dump_unsigned("flush_tid", flush_tid
);
606 void Inode::set_async_err(int r
)
608 for (const auto &fh
: fhs
) {
613 bool Inode::has_recalled_deleg()
615 if (delegations
.empty())
618 // Either all delegations are recalled or none are. Just check the first.
619 Delegation
& deleg
= delegations
.front();
620 return deleg
.is_recalled();
623 void Inode::recall_deleg(bool skip_read
)
625 if (delegations
.empty())
629 for (list
<Delegation
>::iterator d
= delegations
.begin();
630 d
!= delegations
.end(); ++d
) {
632 Delegation
& deleg
= *d
;
633 deleg
.recall(skip_read
);
637 bool Inode::delegations_broken(bool skip_read
)
639 if (delegations
.empty()) {
640 lsubdout(client
->cct
, client
, 10) <<
641 __func__
<< ": delegations empty on " << *this << dendl
;
646 Delegation
& deleg
= delegations
.front();
647 lsubdout(client
->cct
, client
, 10) <<
648 __func__
<< ": read delegs only on " << *this << dendl
;
649 if (deleg
.get_type() == CEPH_FILE_MODE_RD
) {
653 lsubdout(client
->cct
, client
, 10) <<
654 __func__
<< ": not broken" << *this << dendl
;
658 void Inode::break_deleg(bool skip_read
)
660 lsubdout(client
->cct
, client
, 10) <<
661 __func__
<< ": breaking delegs on " << *this << dendl
;
663 recall_deleg(skip_read
);
665 while (!delegations_broken(skip_read
))
666 client
->wait_on_list(waitfor_deleg
);
670 * set_deleg: request a delegation on an open Fh
671 * @fh: filehandle on which to acquire it
672 * @type: delegation request type
673 * @cb: delegation recall callback function
674 * @priv: private pointer to be passed to callback
676 * Attempt to acquire a delegation on an open file handle. If there are no
677 * conflicts and we have the right caps, allocate a new delegation, fill it
678 * out and return 0. Return an error if we can't get one for any reason.
680 int Inode::set_deleg(Fh
*fh
, unsigned type
, ceph_deleg_cb_t cb
, void *priv
)
682 lsubdout(client
->cct
, client
, 10) <<
683 __func__
<< ": inode " << *this << dendl
;
686 * 0 deleg timeout means that they haven't been explicitly enabled. Don't
687 * allow it, with an unusual error to make it clear.
689 if (!client
->get_deleg_timeout())
690 return -CEPHFS_ETIME
;
692 // Just say no if we have any recalled delegs still outstanding
693 if (has_recalled_deleg()) {
694 lsubdout(client
->cct
, client
, 10) << __func__
<<
695 ": has_recalled_deleg" << dendl
;
696 return -CEPHFS_EAGAIN
;
699 // check vs. currently open files on this inode
701 case CEPH_DELEGATION_RD
:
702 if (open_count_for_write()) {
703 lsubdout(client
->cct
, client
, 10) << __func__
<<
704 ": open for write" << dendl
;
705 return -CEPHFS_EAGAIN
;
708 case CEPH_DELEGATION_WR
:
709 if (open_count() > 1) {
710 lsubdout(client
->cct
, client
, 10) << __func__
<< ": open" << dendl
;
711 return -CEPHFS_EAGAIN
;
715 return -CEPHFS_EINVAL
;
719 * A delegation is essentially a long-held container for cap references that
720 * we delegate to the client until recalled. The caps required depend on the
721 * type of delegation (read vs. rw). This is entirely an opportunistic thing.
722 * If we don't have the necessary caps for the delegation, then we just don't
725 * In principle we could request the caps from the MDS, but a delegation is
726 * usually requested just after an open. If we don't have the necessary caps
727 * already, then it's likely that there is some sort of conflicting access.
729 * In the future, we may need to add a way to have this request caps more
730 * aggressively -- for instance, to handle WANT_DELEGATION for NFSv4.1+.
732 int need
= ceph_deleg_caps_for_type(type
);
733 if (!caps_issued_mask(need
)) {
734 lsubdout(client
->cct
, client
, 10) << __func__
<< ": cap mismatch, have="
735 << ccap_string(caps_issued()) << " need=" << ccap_string(need
) << dendl
;
736 return -CEPHFS_EAGAIN
;
739 for (list
<Delegation
>::iterator d
= delegations
.begin();
740 d
!= delegations
.end(); ++d
) {
741 Delegation
& deleg
= *d
;
742 if (deleg
.get_fh() == fh
) {
743 deleg
.reinit(type
, cb
, priv
);
748 delegations
.emplace_back(fh
, type
, cb
, priv
);
753 * unset_deleg - remove a delegation that was previously set
754 * @fh: file handle to clear delegation of
756 * Unlink delegation from the Inode (if there is one), put caps and free it.
758 void Inode::unset_deleg(Fh
*fh
)
760 for (list
<Delegation
>::iterator d
= delegations
.begin();
761 d
!= delegations
.end(); ++d
) {
762 Delegation
& deleg
= *d
;
763 if (deleg
.get_fh() == fh
) {
764 delegations
.erase(d
);
765 client
->signal_cond_list(waitfor_deleg
);
772 * mark_caps_dirty - mark some caps dirty
773 * @caps: the dirty caps
775 * note that if there is no dirty and flushing caps before, we need to pin this inode.
776 * it will be unpined by handle_cap_flush_ack when there are no dirty and flushing caps.
778 void Inode::mark_caps_dirty(int caps
)
781 * If auth_cap is nullptr means the reonnecting is not finished or
785 ceph_assert(!dirty_caps
);
787 lsubdout(client
->cct
, client
, 1) << __func__
<< " " << *this << " dirty caps '" << ccap_string(caps
)
788 << "', but no auth cap." << dendl
;
792 lsubdout(client
->cct
, client
, 10) << __func__
<< " " << *this << " " << ccap_string(dirty_caps
) << " -> "
793 << ccap_string(dirty_caps
| caps
) << dendl
;
795 if (caps
&& !caps_dirty())
799 auth_cap
->session
->get_dirty_list().push_back(&dirty_cap_item
);
800 client
->cap_delay_requeue(this);
804 * mark_caps_clean - only clean the dirty_caps and caller should start flushing the dirty caps.
806 void Inode::mark_caps_clean()
808 lsubdout(client
->cct
, client
, 10) << __func__
<< " " << *this << dendl
;
810 dirty_cap_item
.remove_myself();