]>
git.proxmox.com Git - ceph.git/blob - ceph/src/client/Inode.cc
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"
17 cap_item
.remove_myself();
18 snaprealm_item
.remove_myself();
21 snapdir_parent
->flags
&= ~I_SNAPDIR_OPEN
;
22 snapdir_parent
.reset();
25 if (!oset
.objects
.empty()) {
26 lsubdout(client
->cct
, client
, 0) << __func__
<< ": leftover objects on inode 0x"
27 << std::hex
<< ino
<< std::dec
<< dendl
;
28 assert(oset
.objects
.empty());
31 if (!delegations
.empty()) {
32 lsubdout(client
->cct
, client
, 0) << __func__
<< ": leftover delegations on inode 0x"
33 << std::hex
<< ino
<< std::dec
<< dendl
;
34 assert(delegations
.empty());
41 ostream
& operator<<(ostream
&out
, const Inode
&in
)
43 out
<< in
.vino() << "("
44 << "faked_ino=" << in
.faked_ino
46 << " ll_ref=" << in
.ll_ref
47 << " cap_refs=" << in
.cap_refs
48 << " open=" << in
.open_by_mode
49 << " mode=" << oct
<< in
.mode
<< dec
50 << " size=" << in
.size
<< "/" << in
.max_size
51 << " mtime=" << in
.mtime
52 << " caps=" << ccap_string(in
.caps_issued());
53 if (!in
.caps
.empty()) {
55 for (auto p
= in
.caps
.begin(); p
!= in
.caps
.end(); ++p
) {
56 if (p
!= in
.caps
.begin())
58 out
<< p
->first
<< '=' << ccap_string(p
->second
->issued
);
63 out
<< " dirty_caps=" << ccap_string(in
.dirty_caps
);
65 out
<< " flushing_caps=" << ccap_string(in
.flushing_caps
);
67 if (in
.flags
& I_COMPLETE
)
71 out
<< " " << in
.oset
;
73 if (!in
.dn_set
.empty())
74 out
<< " parents=" << in
.dn_set
;
76 if (in
.is_dir() && in
.has_dir_layout())
77 out
<< " has_dir_layout";
79 if (in
.quota
.is_enable())
80 out
<< " " << in
.quota
;
82 out
<< ' ' << &in
<< ")";
87 void Inode::make_long_path(filepath
& p
)
89 if (!dn_set
.empty()) {
90 assert((*dn_set
.begin())->dir
&& (*dn_set
.begin())->dir
->parent_inode
);
91 (*dn_set
.begin())->dir
->parent_inode
->make_long_path(p
);
92 p
.push_dentry((*dn_set
.begin())->name
);
93 } else if (snapdir_parent
) {
94 snapdir_parent
->make_nosnap_relative_path(p
);
102 * make a filepath suitable for an mds request:
103 * - if we are non-snapped/live, the ino is sufficient, e.g. #1234
104 * - if we are snapped, make filepath relative to first non-snapped parent.
106 void Inode::make_nosnap_relative_path(filepath
& p
)
108 if (snapid
== CEPH_NOSNAP
) {
110 } else if (snapdir_parent
) {
111 snapdir_parent
->make_nosnap_relative_path(p
);
113 p
.push_dentry(empty
);
114 } else if (!dn_set
.empty()) {
115 assert((*dn_set
.begin())->dir
&& (*dn_set
.begin())->dir
->parent_inode
);
116 (*dn_set
.begin())->dir
->parent_inode
->make_nosnap_relative_path(p
);
117 p
.push_dentry((*dn_set
.begin())->name
);
123 void Inode::get_open_ref(int mode
)
125 open_by_mode
[mode
]++;
126 break_deleg(!(mode
& CEPH_FILE_MODE_WR
));
129 bool Inode::put_open_ref(int mode
)
131 //cout << "open_by_mode[" << mode << "] " << open_by_mode[mode] << " -> " << (open_by_mode[mode]-1) << std::endl;
132 if (--open_by_mode
[mode
] == 0)
137 void Inode::get_cap_ref(int cap
)
144 //cout << "inode " << *this << " get " << cap_string(c) << " " << (cap_refs[c]-1) << " -> " << cap_refs[c] << std::endl;
151 int Inode::put_cap_ref(int cap
)
158 if (cap_refs
[c
] <= 0) {
159 lderr(client
->cct
) << "put_cap_ref " << ccap_string(c
) << " went negative on " << *this << dendl
;
160 assert(cap_refs
[c
] > 0);
162 if (--cap_refs
[c
] == 0)
164 //cout << "inode " << *this << " put " << cap_string(c) << " " << (cap_refs[c]+1) << " -> " << cap_refs[c] << std::endl;
172 bool Inode::is_any_caps()
174 return !caps
.empty() || snap_caps
;
177 bool Inode::cap_is_valid(Cap
* cap
) const
179 /*cout << "cap_gen " << cap->session-> cap_gen << std::endl
180 << "session gen " << cap->gen << std::endl
181 << "cap expire " << cap->session->cap_ttl << std::endl
182 << "cur time " << ceph_clock_now(cct) << std::endl;*/
183 if ((cap
->session
->cap_gen
<= cap
->gen
)
184 && (ceph_clock_now() < cap
->session
->cap_ttl
)) {
190 int Inode::caps_issued(int *implemented
) const
194 for (map
<mds_rank_t
,Cap
*>::const_iterator it
= caps
.begin();
197 if (cap_is_valid(it
->second
)) {
198 c
|= it
->second
->issued
;
199 i
|= it
->second
->implemented
;
202 // exclude caps issued by non-auth MDS, but are been revoking by
203 // the auth MDS. The non-auth MDS should be revoking/exporting
204 // these caps, but the message is delayed.
206 c
&= ~auth_cap
->implemented
| auth_cap
->issued
;
213 void Inode::touch_cap(Cap
*cap
)
215 // move to back of LRU
216 cap
->session
->caps
.push_back(&cap
->cap_item
);
219 void Inode::try_touch_cap(mds_rank_t mds
)
222 touch_cap(caps
[mds
]);
226 * caps_issued_mask - check whether we have all of the caps in the mask
227 * @mask: mask to check against
228 * @allow_impl: whether the caller can also use caps that are implemented but not issued
230 * This is the bog standard "check whether we have the required caps" operation.
231 * Typically, we only check against the capset that is currently "issued".
232 * In other words, we ignore caps that have been revoked but not yet released.
234 * Some callers (particularly those doing attribute retrieval) can also make
235 * use of the full set of "implemented" caps to satisfy requests from the
238 * Those callers should refrain from taking new references to implemented
241 bool Inode::caps_issued_mask(unsigned mask
, bool allow_impl
)
246 if ((c
& mask
) == mask
)
250 cap_is_valid(auth_cap
) &&
251 (auth_cap
->issued
& mask
) == mask
) {
256 for (map
<mds_rank_t
,Cap
*>::iterator it
= caps
.begin();
259 if (cap_is_valid(it
->second
)) {
260 if ((it
->second
->issued
& mask
) == mask
) {
261 touch_cap(it
->second
);
264 c
|= it
->second
->issued
;
265 i
|= it
->second
->implemented
;
272 if ((c
& mask
) == mask
) {
273 // bah.. touch them all
274 for (map
<mds_rank_t
,Cap
*>::iterator it
= caps
.begin();
277 touch_cap(it
->second
);
283 int Inode::caps_used()
286 for (map
<int,int>::iterator p
= cap_refs
.begin();
294 int Inode::caps_file_wanted()
297 for (map
<int,int>::iterator p
= open_by_mode
.begin();
298 p
!= open_by_mode
.end();
301 want
|= ceph_caps_for_mode(p
->first
);
305 int Inode::caps_wanted()
307 int want
= caps_file_wanted() | caps_used();
308 if (want
& CEPH_CAP_FILE_BUFFER
)
309 want
|= CEPH_CAP_FILE_EXCL
;
313 int Inode::caps_mds_wanted()
316 for (auto it
= caps
.begin(); it
!= caps
.end(); ++it
)
317 want
|= it
->second
->wanted
;
321 int Inode::caps_dirty()
323 return dirty_caps
| flushing_caps
;
326 const UserPerm
* Inode::get_best_perms()
328 const UserPerm
*perms
= NULL
;
329 for (const auto ci
: caps
) {
330 const UserPerm
& iperm
= ci
.second
->latest_perms
;
331 if (!perms
) { // we don't have any, take what's present
333 } else if (iperm
.uid() == uid
) {
334 if (iperm
.gid() == gid
) { // we have the best possible, return
337 if (perms
->uid() != uid
) { // take uid > gid every time
340 } else if (perms
->uid() != uid
&& iperm
.gid() == gid
) {
341 perms
= &iperm
; // a matching gid is better than nothing
347 bool Inode::have_valid_size()
349 // RD+RDCACHE or WR+WRBUFFER => valid size
350 if (caps_issued() & (CEPH_CAP_FILE_SHARED
| CEPH_CAP_FILE_EXCL
))
355 // open Dir for an inode. if it's not open, allocated it (and pin dentry in memory).
356 Dir
*Inode::open_dir()
360 lsubdout(client
->cct
, client
, 15) << "open_dir " << dir
<< " on " << this << dendl
;
361 assert(dn_set
.size() < 2); // dirs can't be hard-linked
363 (*dn_set
.begin())->get(); // pin dentry
369 bool Inode::check_mode(const UserPerm
& perms
, unsigned want
)
371 if (uid
== perms
.uid()) {
372 // if uid is owner, owner entry determines access
374 } else if (perms
.gid_in_groups(gid
)) {
375 // if a gid or sgid matches the owning group, group entry determines access
379 return (mode
& want
) == want
;
384 lsubdout(client
->cct
, client
, 15) << "inode.get on " << this << " " << ino
<< '.' << snapid
385 << " now " << _ref
<< dendl
;
388 //private method to put a reference; see Client::put_inode()
389 int Inode::_put(int n
) {
391 lsubdout(client
->cct
, client
, 15) << "inode.put on " << this << " " << ino
<< '.' << snapid
392 << " now " << _ref
<< dendl
;
398 void Inode::dump(Formatter
*f
) const
400 f
->dump_stream("ino") << ino
;
401 f
->dump_stream("snapid") << snapid
;
403 f
->dump_unsigned("rdev", rdev
);
404 f
->dump_stream("ctime") << ctime
;
405 f
->dump_stream("btime") << btime
;
406 f
->dump_stream("mode") << '0' << std::oct
<< mode
<< std::dec
;
407 f
->dump_unsigned("uid", uid
);
408 f
->dump_unsigned("gid", gid
);
409 f
->dump_int("nlink", nlink
);
411 f
->dump_unsigned("size", size
);
412 f
->dump_unsigned("max_size", max_size
);
413 f
->dump_unsigned("truncate_seq", truncate_seq
);
414 f
->dump_unsigned("truncate_size", truncate_size
);
415 f
->dump_stream("mtime") << mtime
;
416 f
->dump_stream("atime") << atime
;
417 f
->dump_unsigned("time_warp_seq", time_warp_seq
);
418 f
->dump_unsigned("change_attr", change_attr
);
420 f
->dump_object("layout", layout
);
422 f
->open_object_section("dir_layout");
423 ::dump(dir_layout
, f
);
426 f
->dump_bool("complete", flags
& I_COMPLETE
);
427 f
->dump_bool("ordered", flags
& I_DIR_ORDERED
);
429 /* FIXME when wip-mds-encoding is merged ***
430 f->open_object_section("dir_stat");
434 f->open_object_section("rstat");
440 f
->dump_unsigned("version", version
);
441 f
->dump_unsigned("xattr_version", xattr_version
);
442 f
->dump_unsigned("flags", flags
);
445 if (!dir_contacts
.empty()) {
446 f
->open_object_section("dir_contants");
447 for (set
<int>::iterator p
= dir_contacts
.begin(); p
!= dir_contacts
.end(); ++p
)
448 f
->dump_int("mds", *p
);
451 f
->dump_int("dir_hashed", (int)dir_hashed
);
452 f
->dump_int("dir_replicated", (int)dir_replicated
);
455 f
->open_array_section("caps");
456 for (map
<mds_rank_t
,Cap
*>::const_iterator p
= caps
.begin(); p
!= caps
.end(); ++p
) {
457 f
->open_object_section("cap");
458 f
->dump_int("mds", p
->first
);
459 if (p
->second
== auth_cap
)
460 f
->dump_int("auth", 1);
466 f
->dump_int("auth_cap", auth_cap
->session
->mds_num
);
468 f
->dump_stream("dirty_caps") << ccap_string(dirty_caps
);
470 f
->dump_stream("flushings_caps") << ccap_string(flushing_caps
);
471 f
->open_object_section("flushing_cap_tid");
472 for (map
<ceph_tid_t
, int>::const_iterator p
= flushing_cap_tids
.begin();
473 p
!= flushing_cap_tids
.end();
475 string
n(ccap_string(p
->second
));
476 f
->dump_unsigned(n
.c_str(), p
->first
);
480 f
->dump_int("shared_gen", shared_gen
);
481 f
->dump_int("cache_gen", cache_gen
);
483 f
->dump_int("snap_caps", snap_caps
);
484 f
->dump_int("snap_cap_refs", snap_cap_refs
);
487 f
->dump_stream("hold_caps_until") << hold_caps_until
;
490 f
->open_object_section("snaprealm");
494 if (!cap_snaps
.empty()) {
495 for (const auto &p
: cap_snaps
) {
496 f
->open_object_section("cap_snap");
497 f
->dump_stream("follows") << p
.first
;
504 if (!open_by_mode
.empty()) {
505 f
->open_array_section("open_by_mode");
506 for (map
<int,int>::const_iterator p
= open_by_mode
.begin(); p
!= open_by_mode
.end(); ++p
) {
507 f
->open_object_section("ref");
508 f
->dump_int("mode", p
->first
);
509 f
->dump_int("refs", p
->second
);
514 if (!cap_refs
.empty()) {
515 f
->open_array_section("cap_refs");
516 for (map
<int,int>::const_iterator p
= cap_refs
.begin(); p
!= cap_refs
.end(); ++p
) {
517 f
->open_object_section("cap_ref");
518 f
->dump_stream("cap") << ccap_string(p
->first
);
519 f
->dump_int("refs", p
->second
);
525 f
->dump_unsigned("reported_size", reported_size
);
526 if (wanted_max_size
!= max_size
)
527 f
->dump_unsigned("wanted_max_size", wanted_max_size
);
528 if (requested_max_size
!= max_size
)
529 f
->dump_unsigned("requested_max_size", requested_max_size
);
531 f
->dump_int("ref", _ref
);
532 f
->dump_int("ll_ref", ll_ref
);
534 if (!dn_set
.empty()) {
535 f
->open_array_section("parents");
536 for (set
<Dentry
*>::const_iterator p
= dn_set
.begin(); p
!= dn_set
.end(); ++p
) {
537 f
->open_object_section("dentry");
538 f
->dump_stream("dir_ino") << (*p
)->dir
->parent_inode
->ino
;
539 f
->dump_string("name", (*p
)->name
);
546 void Cap::dump(Formatter
*f
) const
548 f
->dump_int("mds", session
->mds_num
);
549 f
->dump_stream("ino") << inode
->ino
;
550 f
->dump_unsigned("cap_id", cap_id
);
551 f
->dump_stream("issued") << ccap_string(issued
);
552 if (implemented
!= issued
)
553 f
->dump_stream("implemented") << ccap_string(implemented
);
554 f
->dump_stream("wanted") << ccap_string(wanted
);
555 f
->dump_unsigned("seq", seq
);
556 f
->dump_unsigned("issue_seq", issue_seq
);
557 f
->dump_unsigned("mseq", mseq
);
558 f
->dump_unsigned("gen", gen
);
561 void CapSnap::dump(Formatter
*f
) const
563 f
->dump_stream("ino") << in
->ino
;
564 f
->dump_stream("issued") << ccap_string(issued
);
565 f
->dump_stream("dirty") << ccap_string(dirty
);
566 f
->dump_unsigned("size", size
);
567 f
->dump_stream("ctime") << ctime
;
568 f
->dump_stream("mtime") << mtime
;
569 f
->dump_stream("atime") << atime
;
570 f
->dump_int("time_warp_seq", time_warp_seq
);
571 f
->dump_stream("mode") << '0' << std::oct
<< mode
<< std::dec
;
572 f
->dump_unsigned("uid", uid
);
573 f
->dump_unsigned("gid", gid
);
574 if (!xattrs
.empty()) {
575 f
->open_object_section("xattr_lens");
576 for (map
<string
,bufferptr
>::const_iterator p
= xattrs
.begin(); p
!= xattrs
.end(); ++p
)
577 f
->dump_int(p
->first
.c_str(), p
->second
.length());
580 f
->dump_unsigned("xattr_version", xattr_version
);
581 f
->dump_int("writing", (int)writing
);
582 f
->dump_int("dirty_data", (int)dirty_data
);
583 f
->dump_unsigned("flush_tid", flush_tid
);
586 void Inode::set_async_err(int r
)
588 for (const auto &fh
: fhs
) {
593 bool Inode::has_recalled_deleg()
595 if (delegations
.empty())
598 // Either all delegations are recalled or none are. Just check the first.
599 Delegation
& deleg
= delegations
.front();
600 return deleg
.is_recalled();
603 void Inode::recall_deleg(bool skip_read
)
605 if (delegations
.empty())
609 for (list
<Delegation
>::iterator d
= delegations
.begin();
610 d
!= delegations
.end(); ++d
) {
612 Delegation
& deleg
= *d
;
613 deleg
.recall(skip_read
);
617 bool Inode::delegations_broken(bool skip_read
)
619 if (delegations
.empty()) {
620 lsubdout(client
->cct
, client
, 10) <<
621 __func__
<< ": delegations empty on " << *this << dendl
;
626 Delegation
& deleg
= delegations
.front();
627 lsubdout(client
->cct
, client
, 10) <<
628 __func__
<< ": read delegs only on " << *this << dendl
;
629 if (deleg
.get_type() == CEPH_FILE_MODE_RD
) {
633 lsubdout(client
->cct
, client
, 10) <<
634 __func__
<< ": not broken" << *this << dendl
;
638 void Inode::break_deleg(bool skip_read
)
640 lsubdout(client
->cct
, client
, 10) <<
641 __func__
<< ": breaking delegs on " << *this << dendl
;
643 recall_deleg(skip_read
);
645 while (!delegations_broken(skip_read
))
646 client
->wait_on_list(waitfor_deleg
);
650 * set_deleg: request a delegation on an open Fh
651 * @fh: filehandle on which to acquire it
652 * @type: delegation request type
653 * @cb: delegation recall callback function
654 * @priv: private pointer to be passed to callback
656 * Attempt to acquire a delegation on an open file handle. If there are no
657 * conflicts and we have the right caps, allocate a new delegation, fill it
658 * out and return 0. Return an error if we can't get one for any reason.
660 int Inode::set_deleg(Fh
*fh
, unsigned type
, ceph_deleg_cb_t cb
, void *priv
)
662 lsubdout(client
->cct
, client
, 10) <<
663 __func__
<< ": inode " << *this << dendl
;
666 * 0 deleg timeout means that they haven't been explicitly enabled. Don't
667 * allow it, with an unusual error to make it clear.
669 if (!client
->get_deleg_timeout())
672 // Just say no if we have any recalled delegs still outstanding
673 if (has_recalled_deleg()) {
674 lsubdout(client
->cct
, client
, 10) << __func__
<<
675 ": has_recalled_deleg" << dendl
;
679 // check vs. currently open files on this inode
681 case CEPH_DELEGATION_RD
:
682 if (open_count_for_write()) {
683 lsubdout(client
->cct
, client
, 10) << __func__
<<
684 ": open for write" << dendl
;
688 case CEPH_DELEGATION_WR
:
689 if (open_count() > 1) {
690 lsubdout(client
->cct
, client
, 10) << __func__
<< ": open" << dendl
;
699 * A delegation is essentially a long-held container for cap references that
700 * we delegate to the client until recalled. The caps required depend on the
701 * type of delegation (read vs. rw). This is entirely an opportunistic thing.
702 * If we don't have the necessary caps for the delegation, then we just don't
705 * In principle we could request the caps from the MDS, but a delegation is
706 * usually requested just after an open. If we don't have the necessary caps
707 * already, then it's likely that there is some sort of conflicting access.
709 * In the future, we may need to add a way to have this request caps more
710 * aggressively -- for instance, to handle WANT_DELEGATION for NFSv4.1+.
712 int need
= ceph_deleg_caps_for_type(type
);
713 if (!caps_issued_mask(need
)) {
714 lsubdout(client
->cct
, client
, 10) << __func__
<< ": cap mismatch, have="
715 << ccap_string(caps_issued()) << " need=" << ccap_string(need
) << dendl
;
719 for (list
<Delegation
>::iterator d
= delegations
.begin();
720 d
!= delegations
.end(); ++d
) {
721 Delegation
& deleg
= *d
;
722 if (deleg
.get_fh() == fh
) {
723 deleg
.reinit(type
, cb
, priv
);
728 delegations
.emplace_back(fh
, type
, cb
, priv
);
733 * unset_deleg - remove a delegation that was previously set
734 * @fh: file handle to clear delegation of
736 * Unlink delegation from the Inode (if there is one), put caps and free it.
738 void Inode::unset_deleg(Fh
*fh
)
740 for (list
<Delegation
>::iterator d
= delegations
.begin();
741 d
!= delegations
.end(); ++d
) {
742 Delegation
& deleg
= *d
;
743 if (deleg
.get_fh() == fh
) {
744 delegations
.erase(d
);
745 client
->signal_cond_list(waitfor_deleg
);