1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "include/compat.h"
5 #include "include/rados/rgw_file.h"
11 #include "rgw_rados.h"
12 #include "rgw_resolve.h"
16 #include "rgw_acl_s3.h"
17 #include "rgw_frontend.h"
18 #include "rgw_request.h"
19 #include "rgw_process.h"
20 #include "rgw_rest_user.h"
21 #include "rgw_rest_s3.h"
22 #include "rgw_os_lib.h"
23 #include "rgw_auth_s3.h"
25 #include "rgw_bucket.h"
28 #include "rgw_lib_frontend.h"
32 #define dout_subsys ceph_subsys_rgw
40 const string
RGWFileHandle::root_name
= "/";
42 std::atomic
<uint32_t> RGWLibFS::fs_inst_counter
;
44 uint32_t RGWLibFS::write_completion_interval_s
= 10;
46 ceph::timer
<ceph::mono_clock
> RGWLibFS::write_timer
{
47 ceph::construct_suspended
};
49 inline int valid_fs_bucket_name(const string
& name
) {
50 int rc
= valid_s3_bucket_name(name
, false /* relaxed */);
52 if (name
.size() > 255)
59 inline int valid_fs_object_name(const string
& name
) {
60 int rc
= valid_s3_object_name(name
);
62 if (name
.size() > 1024)
69 LookupFHResult
RGWLibFS::stat_bucket(RGWFileHandle
* parent
, const char *path
,
70 RGWLibFS::BucketStats
& bs
,
73 LookupFHResult fhr
{nullptr, 0};
74 std::string bucket_name
{path
};
75 RGWStatBucketRequest
req(cct
, get_user(), bucket_name
, bs
);
77 int rc
= rgwlib
.get_fe()->execute_req(&req
);
79 (req
.get_ret() == 0) &&
81 fhr
= lookup_fh(parent
, path
,
82 (flags
& RGWFileHandle::FLAG_LOCKED
)|
83 RGWFileHandle::FLAG_CREATE
|
84 RGWFileHandle::FLAG_BUCKET
);
86 RGWFileHandle
* rgw_fh
= get
<0>(fhr
);
87 if (! (flags
& RGWFileHandle::FLAG_LOCKED
)) {
90 rgw_fh
->set_times(req
.get_ctime());
91 /* restore attributes */
92 auto ux_key
= req
.get_attr(RGW_ATTR_UNIX_KEY1
);
93 auto ux_attrs
= req
.get_attr(RGW_ATTR_UNIX1
);
94 if (ux_key
&& ux_attrs
) {
95 DecodeAttrsResult dar
= rgw_fh
->decode_attrs(ux_key
, ux_attrs
);
96 if (get
<0>(dar
) || get
<1>(dar
)) {
100 if (! (flags
& RGWFileHandle::FLAG_LOCKED
)) {
101 rgw_fh
->mtx
.unlock();
108 LookupFHResult
RGWLibFS::stat_leaf(RGWFileHandle
* parent
,
110 enum rgw_fh_type type
,
113 /* find either-of <object_name>, <object_name/>, only one of
114 * which should exist; atomicity? */
117 LookupFHResult fhr
{nullptr, 0};
119 /* XXX the need for two round-trip operations to identify file or
120 * directory leaf objects is unecessary--the current proposed
121 * mechanism to avoid this is to store leaf object names with an
122 * object locator w/o trailing slash */
124 std::string obj_path
= parent
->format_child_name(path
, false);
126 for (auto ix
: { 0, 1, 2 }) {
131 if (type
== RGW_FS_TYPE_DIRECTORY
)
134 RGWStatObjRequest
req(cct
, get_user(),
135 parent
->bucket_name(), obj_path
,
136 RGWStatObjRequest::FLAG_NONE
);
137 int rc
= rgwlib
.get_fe()->execute_req(&req
);
139 (req
.get_ret() == 0)) {
140 fhr
= lookup_fh(parent
, path
, RGWFileHandle::FLAG_CREATE
);
142 RGWFileHandle
* rgw_fh
= get
<0>(fhr
);
143 lock_guard
guard(rgw_fh
->mtx
);
144 rgw_fh
->set_size(req
.get_size());
145 rgw_fh
->set_times(req
.get_mtime());
146 /* restore attributes */
147 auto ux_key
= req
.get_attr(RGW_ATTR_UNIX_KEY1
);
148 auto ux_attrs
= req
.get_attr(RGW_ATTR_UNIX1
);
149 if (ux_key
&& ux_attrs
) {
150 DecodeAttrsResult dar
= rgw_fh
->decode_attrs(ux_key
, ux_attrs
);
151 if (get
<0>(dar
) || get
<1>(dar
)) {
164 if (type
== RGW_FS_TYPE_FILE
)
168 RGWStatObjRequest
req(cct
, get_user(),
169 parent
->bucket_name(), obj_path
,
170 RGWStatObjRequest::FLAG_NONE
);
171 int rc
= rgwlib
.get_fe()->execute_req(&req
);
173 (req
.get_ret() == 0)) {
174 fhr
= lookup_fh(parent
, path
, RGWFileHandle::FLAG_DIRECTORY
);
176 RGWFileHandle
* rgw_fh
= get
<0>(fhr
);
177 lock_guard
guard(rgw_fh
->mtx
);
178 rgw_fh
->set_size(req
.get_size());
179 rgw_fh
->set_times(req
.get_mtime());
180 /* restore attributes */
181 auto ux_key
= req
.get_attr(RGW_ATTR_UNIX_KEY1
);
182 auto ux_attrs
= req
.get_attr(RGW_ATTR_UNIX1
);
183 if (ux_key
&& ux_attrs
) {
184 DecodeAttrsResult dar
= rgw_fh
->decode_attrs(ux_key
, ux_attrs
);
185 if (get
<0>(dar
) || get
<1>(dar
)) {
196 std::string object_name
{path
};
197 RGWStatLeafRequest
req(cct
, get_user(), parent
, object_name
);
198 int rc
= rgwlib
.get_fe()->execute_req(&req
);
200 (req
.get_ret() == 0)) {
202 /* we need rgw object's key name equal to file name, if
204 if ((flags
& RGWFileHandle::FLAG_EXACT_MATCH
) &&
205 !req
.exact_matched
) {
206 lsubdout(get_context(), rgw
, 15)
208 << ": stat leaf not exact match file name = "
212 fhr
= lookup_fh(parent
, path
,
213 RGWFileHandle::FLAG_CREATE
|
215 RGWFileHandle::FLAG_DIRECTORY
:
216 RGWFileHandle::FLAG_NONE
));
217 /* XXX we don't have an object--in general, there need not
218 * be one (just a path segment in some other object). In
219 * actual leaf an object exists, but we'd need another round
220 * trip to get attrs */
222 /* for now use the parent object's mtime */
223 RGWFileHandle
* rgw_fh
= get
<0>(fhr
);
224 lock_guard
guard(rgw_fh
->mtx
);
225 rgw_fh
->set_mtime(parent
->get_mtime());
238 } /* RGWLibFS::stat_leaf */
240 int RGWLibFS::read(RGWFileHandle
* rgw_fh
, uint64_t offset
, size_t length
,
241 size_t* bytes_read
, void* buffer
, uint32_t flags
)
243 if (! rgw_fh
->is_file())
246 if (rgw_fh
->deleted())
249 RGWReadRequest
req(get_context(), get_user(), rgw_fh
, offset
, length
,
252 int rc
= rgwlib
.get_fe()->execute_req(&req
);
254 (req
.get_ret() == 0)) {
255 lock_guard(rgw_fh
->mtx
);
256 rgw_fh
->set_atime(real_clock::to_timespec(real_clock::now()));
257 *bytes_read
= req
.nread
;
263 int RGWLibFS::unlink(RGWFileHandle
* rgw_fh
, const char* name
, uint32_t flags
)
267 RGWFileHandle
* parent
= nullptr;
268 RGWFileHandle
* bkt_fh
= nullptr;
270 if (unlikely(flags
& RGWFileHandle::FLAG_UNLINK_THIS
)) {
272 parent
= rgw_fh
->get_parent();
276 LookupFHResult fhr
= lookup_fh(parent
, name
, RGWFileHandle::FLAG_LOCK
);
277 rgw_fh
= get
<0>(fhr
);
281 if (parent
->is_root()) {
282 /* a bucket may have an object storing Unix attributes, check
283 * for and delete it */
285 fhr
= stat_bucket(parent
, name
, bs
, (rgw_fh
) ?
286 RGWFileHandle::FLAG_LOCKED
:
287 RGWFileHandle::FLAG_NONE
);
288 bkt_fh
= get
<0>(fhr
);
289 if (unlikely(! bkt_fh
)) {
290 /* implies !rgw_fh, so also !LOCKED */
294 if (bs
.num_entries
> 1) {
295 unref(bkt_fh
); /* return stat_bucket ref */
296 if (likely(!! rgw_fh
)) { /* return lock and ref from
297 * lookup_fh (or caller in the
299 * RGWFileHandle::FLAG_UNLINK_THIS) */
300 rgw_fh
->mtx
.unlock();
305 /* delete object w/key "<bucket>/" (uxattrs), if any */
307 RGWDeleteObjRequest
req(cct
, get_user(), bkt_fh
->bucket_name(), oname
);
308 rc
= rgwlib
.get_fe()->execute_req(&req
);
309 /* don't care if ENOENT */
314 RGWDeleteBucketRequest
req(cct
, get_user(), bname
);
315 rc
= rgwlib
.get_fe()->execute_req(&req
);
324 /* XXX for now, peform a hard lookup to deduce the type of
325 * object to be deleted ("foo" vs. "foo/")--also, ensures
326 * atomicity at this endpoint */
327 struct rgw_file_handle
*fh
;
328 rc
= rgw_lookup(get_fs(), parent
->get_fh(), name
, &fh
,
329 RGW_LOOKUP_FLAG_NONE
);
334 rgw_fh
= get_rgwfh(fh
);
335 rgw_fh
->mtx
.lock(); /* LOCKED */
338 std::string oname
= rgw_fh
->relative_object_name();
339 if (rgw_fh
->is_dir()) {
340 /* for the duration of our cache timer, trust positive
342 if (rgw_fh
->has_children()) {
343 rgw_fh
->mtx
.unlock();
349 RGWDeleteObjRequest
req(cct
, get_user(), parent
->bucket_name(),
351 rc
= rgwlib
.get_fe()->execute_req(&req
);
357 /* ENOENT when raced with other s3 gateway */
358 if (! rc
|| rc
== -ENOENT
) {
359 rgw_fh
->flags
|= RGWFileHandle::FLAG_DELETED
;
360 fh_cache
.remove(rgw_fh
->fh
.fh_hk
.object
, rgw_fh
,
361 RGWFileHandle::FHCache::FLAG_LOCK
);
365 real_time t
= real_clock::now();
366 parent
->set_mtime(real_clock::to_timespec(t
));
367 parent
->set_ctime(real_clock::to_timespec(t
));
370 rgw_fh
->mtx
.unlock();
374 } /* RGWLibFS::unlink */
376 int RGWLibFS::rename(RGWFileHandle
* src_fh
, RGWFileHandle
* dst_fh
,
377 const char *_src_name
, const char *_dst_name
)
380 /* XXX initial implementation: try-copy, and delete if copy
386 std::string src_name
{_src_name
};
387 std::string dst_name
{_dst_name
};
390 LookupFHResult fhr
= lookup_fh(src_fh
, _src_name
, RGWFileHandle::FLAG_LOCK
);
391 RGWFileHandle
* rgw_fh
= get
<0>(fhr
);
393 /* should not happen */
395 ldout(get_context(), 0) << __func__
396 << " BUG no such src renaming path="
402 /* forbid renaming of directories (unreasonable at scale) */
403 if (rgw_fh
->is_dir()) {
404 ldout(get_context(), 12) << __func__
405 << " rejecting attempt to rename directory path="
406 << rgw_fh
->full_object_name()
412 /* forbid renaming open files (violates intent, for now) */
413 if (rgw_fh
->is_open()) {
414 ldout(get_context(), 12) << __func__
415 << " rejecting attempt to rename open file path="
416 << rgw_fh
->full_object_name()
422 t
= real_clock::now();
424 for (int ix
: {0, 1}) {
428 RGWCopyObjRequest
req(cct
, get_user(), src_fh
, dst_fh
, src_name
,
430 int rc
= rgwlib
.get_fe()->execute_req(&req
);
432 ((rc
= req
.get_ret()) != 0)) {
433 ldout(get_context(), 1)
435 << " rename step 0 failed src="
436 << src_fh
->full_object_name() << " " << src_name
437 << " dst=" << dst_fh
->full_object_name()
443 ldout(get_context(), 12)
445 << " rename step 0 success src="
446 << src_fh
->full_object_name() << " " << src_name
447 << " dst=" << dst_fh
->full_object_name()
451 /* update dst change id */
452 dst_fh
->set_times(t
);
457 rc
= this->unlink(rgw_fh
/* LOCKED */, _src_name
,
458 RGWFileHandle::FLAG_UNLINK_THIS
);
461 ldout(get_context(), 12)
463 << " rename step 1 success src="
464 << src_fh
->full_object_name() << " " << src_name
465 << " dst=" << dst_fh
->full_object_name()
469 /* update src change id */
470 src_fh
->set_times(t
);
472 ldout(get_context(), 1)
474 << " rename step 1 failed src="
475 << src_fh
->full_object_name() << " " << src_name
476 << " dst=" << dst_fh
->full_object_name()
488 rgw_fh
->mtx
.unlock(); /* !LOCKED */
489 unref(rgw_fh
); /* -ref */
493 } /* RGWLibFS::rename */
495 MkObjResult
RGWLibFS::mkdir(RGWFileHandle
* parent
, const char *name
,
496 struct stat
*st
, uint32_t mask
, uint32_t flags
)
499 rgw_file_handle
*lfh
;
501 rc
= rgw_lookup(get_fs(), parent
->get_fh(), name
, &lfh
,
502 RGW_LOOKUP_FLAG_NONE
);
505 rc
= rgw_fh_rele(get_fs(), lfh
, RGW_FH_RELE_FLAG_NONE
);
506 return MkObjResult
{nullptr, -EEXIST
};
509 MkObjResult mkr
{nullptr, -EINVAL
};
511 RGWFileHandle
* rgw_fh
= nullptr;
512 buffer::list ux_key
, ux_attrs
;
514 fhr
= lookup_fh(parent
, name
,
515 RGWFileHandle::FLAG_CREATE
|
516 RGWFileHandle::FLAG_DIRECTORY
|
517 RGWFileHandle::FLAG_LOCK
);
518 rgw_fh
= get
<0>(fhr
);
520 rgw_fh
->create_stat(st
, mask
);
521 rgw_fh
->set_times(real_clock::now());
523 rgw_fh
->encode_attrs(ux_key
, ux_attrs
);
526 get
<0>(mkr
) = rgw_fh
;
532 if (parent
->is_root()) {
535 /* enforce S3 name restrictions */
536 rc
= valid_fs_bucket_name(bname
);
538 rgw_fh
->flags
|= RGWFileHandle::FLAG_DELETED
;
539 fh_cache
.remove(rgw_fh
->fh
.fh_hk
.object
, rgw_fh
,
540 RGWFileHandle::FHCache::FLAG_LOCK
);
541 rgw_fh
->mtx
.unlock();
543 get
<0>(mkr
) = nullptr;
548 RGWCreateBucketRequest
req(get_context(), get_user(), bname
);
551 req
.emplace_attr(RGW_ATTR_UNIX_KEY1
, std::move(ux_key
));
552 req
.emplace_attr(RGW_ATTR_UNIX1
, std::move(ux_attrs
));
554 rc
= rgwlib
.get_fe()->execute_req(&req
);
557 /* create an object representing the directory */
559 string dir_name
= parent
->format_child_name(name
, true);
561 /* need valid S3 name (characters, length <= 1024, etc) */
562 rc
= valid_fs_object_name(dir_name
);
564 rgw_fh
->flags
|= RGWFileHandle::FLAG_DELETED
;
565 fh_cache
.remove(rgw_fh
->fh
.fh_hk
.object
, rgw_fh
,
566 RGWFileHandle::FHCache::FLAG_LOCK
);
567 rgw_fh
->mtx
.unlock();
569 get
<0>(mkr
) = nullptr;
574 RGWPutObjRequest
req(get_context(), get_user(), parent
->bucket_name(),
578 req
.emplace_attr(RGW_ATTR_UNIX_KEY1
, std::move(ux_key
));
579 req
.emplace_attr(RGW_ATTR_UNIX1
, std::move(ux_attrs
));
581 rc
= rgwlib
.get_fe()->execute_req(&req
);
588 rgw_fh
->flags
|= RGWFileHandle::FLAG_DELETED
;
589 rgw_fh
->mtx
.unlock(); /* !LOCKED */
591 get
<0>(mkr
) = nullptr;
596 real_time t
= real_clock::now();
597 parent
->set_mtime(real_clock::to_timespec(t
));
598 parent
->set_ctime(real_clock::to_timespec(t
));
599 rgw_fh
->mtx
.unlock(); /* !LOCKED */
605 } /* RGWLibFS::mkdir */
607 MkObjResult
RGWLibFS::create(RGWFileHandle
* parent
, const char *name
,
608 struct stat
*st
, uint32_t mask
, uint32_t flags
)
614 rgw_file_handle
*lfh
;
615 rc
= rgw_lookup(get_fs(), parent
->get_fh(), name
, &lfh
,
616 RGW_LOOKUP_FLAG_NONE
);
619 rc
= rgw_fh_rele(get_fs(), lfh
, RGW_FH_RELE_FLAG_NONE
);
620 return MkObjResult
{nullptr, -EEXIST
};
623 /* expand and check name */
624 std::string obj_name
= parent
->format_child_name(name
, false);
625 rc
= valid_fs_object_name(obj_name
);
627 return MkObjResult
{nullptr, rc
};
632 RGWPutObjRequest
req(cct
, get_user(), parent
->bucket_name(), obj_name
, bl
);
633 MkObjResult mkr
{nullptr, -EINVAL
};
635 rc
= rgwlib
.get_fe()->execute_req(&req
);
641 LookupFHResult fhr
= lookup_fh(parent
, name
, RGWFileHandle::FLAG_CREATE
|
642 RGWFileHandle::FLAG_LOCK
);
643 RGWFileHandle
* rgw_fh
= get
<0>(fhr
);
645 if (get
<1>(fhr
) & RGWFileHandle::FLAG_CREATE
) {
646 /* fill in stat data */
647 real_time t
= real_clock::now();
648 rgw_fh
->create_stat(st
, mask
);
649 rgw_fh
->set_times(t
);
651 parent
->set_mtime(real_clock::to_timespec(t
));
652 parent
->set_ctime(real_clock::to_timespec(t
));
655 (void) rgw_fh
->stat(st
);
656 get
<0>(mkr
) = rgw_fh
;
657 rgw_fh
->mtx
.unlock();
665 } /* RGWLibFS::create */
667 int RGWLibFS::getattr(RGWFileHandle
* rgw_fh
, struct stat
* st
)
669 switch(rgw_fh
->fh
.fh_type
) {
670 case RGW_FS_TYPE_FILE
:
672 if (rgw_fh
->deleted())
680 return rgw_fh
->stat(st
);
681 } /* RGWLibFS::getattr */
683 int RGWLibFS::setattr(RGWFileHandle
* rgw_fh
, struct stat
* st
, uint32_t mask
,
687 buffer::list ux_key
, ux_attrs
;
689 lock_guard
guard(rgw_fh
->mtx
);
691 switch(rgw_fh
->fh
.fh_type
) {
692 case RGW_FS_TYPE_FILE
:
694 if (rgw_fh
->deleted())
702 string obj_name
{rgw_fh
->relative_object_name()};
704 if (rgw_fh
->is_dir() &&
705 (likely(! rgw_fh
->is_bucket()))) {
709 RGWSetAttrsRequest
req(cct
, get_user(), rgw_fh
->bucket_name(), obj_name
);
711 rgw_fh
->create_stat(st
, mask
);
712 rgw_fh
->encode_attrs(ux_key
, ux_attrs
);
715 req
.emplace_attr(RGW_ATTR_UNIX_KEY1
, std::move(ux_key
));
716 req
.emplace_attr(RGW_ATTR_UNIX1
, std::move(ux_attrs
));
718 rc
= rgwlib
.get_fe()->execute_req(&req
);
722 /* special case: materialize placeholder dir */
724 RGWPutObjRequest
req(get_context(), get_user(), rgw_fh
->bucket_name(),
727 rgw_fh
->encode_attrs(ux_key
, ux_attrs
); /* because std::moved */
730 req
.emplace_attr(RGW_ATTR_UNIX_KEY1
, std::move(ux_key
));
731 req
.emplace_attr(RGW_ATTR_UNIX1
, std::move(ux_attrs
));
733 rc
= rgwlib
.get_fe()->execute_req(&req
);
737 if ((rc
!= 0) || (rc2
!= 0)) {
741 rgw_fh
->set_ctime(real_clock::to_timespec(real_clock::now()));
744 } /* RGWLibFS::setattr */
746 /* called under rgw_fh->mtx held */
747 void RGWLibFS::update_fh(RGWFileHandle
*rgw_fh
)
750 string obj_name
{rgw_fh
->relative_object_name()};
751 buffer::list ux_key
, ux_attrs
;
753 if (rgw_fh
->is_dir() &&
754 (likely(! rgw_fh
->is_bucket()))) {
758 lsubdout(get_context(), rgw
, 17)
760 << " update old versioned fh : " << obj_name
763 RGWSetAttrsRequest
req(cct
, get_user(), rgw_fh
->bucket_name(), obj_name
);
765 rgw_fh
->encode_attrs(ux_key
, ux_attrs
);
767 req
.emplace_attr(RGW_ATTR_UNIX_KEY1
, std::move(ux_key
));
768 req
.emplace_attr(RGW_ATTR_UNIX1
, std::move(ux_attrs
));
770 rc
= rgwlib
.get_fe()->execute_req(&req
);
773 if ((rc
!= 0) || (rc2
!= 0)) {
774 lsubdout(get_context(), rgw
, 17)
776 << " update fh failed : " << obj_name
779 } /* RGWLibFS::update_fh */
781 void RGWLibFS::close()
783 state
.flags
|= FLAG_CLOSED
;
789 ObjUnref(RGWLibFS
* _fs
) : fs(_fs
) {}
790 void operator()(RGWFileHandle
* fh
) const {
791 lsubdout(fs
->get_context(), rgw
, 5)
794 << " before ObjUnref refs=" << fh
->get_refcnt()
800 /* force cache drain, forces objects to evict */
801 fh_cache
.drain(ObjUnref(this),
802 RGWFileHandle::FHCache::FLAG_LOCK
);
803 rgwlib
.get_fe()->get_process()->unregister_fs(this);
805 } /* RGWLibFS::close */
807 inline std::ostream
& operator<<(std::ostream
&os
, struct timespec
const &ts
) {
808 os
<< "<timespec: tv_sec=";
816 std::ostream
& operator<<(std::ostream
&os
, RGWLibFS::event
const &ev
) {
819 case RGWLibFS::event::type::READDIR
:
820 os
<< "type=READDIR;";
823 os
<< "type=UNKNOWN;";
826 os
<< "fid=" << ev
.fhk
.fh_hk
.bucket
<< ":" << ev
.fhk
.fh_hk
.object
827 << ";ts=" << ev
.ts
<< ">";
834 using directory
= RGWFileHandle::directory
;
836 /* dirent invalidate timeout--basically, the upper-bound on
837 * inconsistency with the S3 namespace */
839 = get_context()->_conf
->rgw_nfs_namespace_expire_secs
;
841 /* max events to gc in one cycle */
842 uint32_t max_ev
= get_context()->_conf
->rgw_nfs_max_gc
;
844 struct timespec now
, expire_ts
;
847 std::deque
<event
> &events
= state
.events
;
850 (void) clock_gettime(CLOCK_MONOTONIC_COARSE
, &now
);
851 lsubdout(get_context(), rgw
, 15)
852 << "GC: top of expire loop"
854 << " expire_s=" << expire_s
857 lock_guard
guard(state
.mtx
); /* LOCKED */
858 /* just return if no events */
859 if (events
.empty()) {
863 (events
.size() < 500) ? max_ev
: (events
.size() / 4);
864 for (uint32_t ix
= 0; (ix
< _max_ev
) && (events
.size() > 0); ++ix
) {
865 event
& ev
= events
.front();
867 expire_ts
.tv_sec
+= expire_s
;
868 if (expire_ts
> now
) {
877 for (auto& ev
: ve
) {
878 lsubdout(get_context(), rgw
, 15)
879 << "try-expire ev: " << ev
<< dendl
;
880 if (likely(ev
.t
== event::type::READDIR
)) {
881 RGWFileHandle
* rgw_fh
= lookup_handle(ev
.fhk
.fh_hk
);
882 lsubdout(get_context(), rgw
, 15)
883 << "ev rgw_fh: " << rgw_fh
<< dendl
;
885 RGWFileHandle::directory
* d
;
886 if (unlikely(! rgw_fh
->is_dir())) {
887 lsubdout(get_context(), rgw
, 0)
889 << " BUG non-directory found with READDIR event "
890 << "(" << rgw_fh
->bucket_name() << ","
891 << rgw_fh
->object_name() << ")"
895 /* maybe clear state */
896 d
= get
<directory
>(&rgw_fh
->variant_type
);
898 struct timespec ev_ts
= ev
.ts
;
899 lock_guard
guard(rgw_fh
->mtx
);
900 struct timespec d_last_readdir
= d
->last_readdir
;
901 if (unlikely(ev_ts
< d_last_readdir
)) {
902 /* readdir cycle in progress, don't invalidate */
903 lsubdout(get_context(), rgw
, 15)
904 << "GC: delay expiration for "
905 << rgw_fh
->object_name()
906 << " ev.ts=" << ev_ts
907 << " last_readdir=" << d_last_readdir
911 lsubdout(get_context(), rgw
, 15)
913 << rgw_fh
->object_name()
915 rgw_fh
->clear_state();
916 rgw_fh
->invalidate();
922 } /* event::type::READDIR */
925 } while (! (stop
|| shutdown
));
928 std::ostream
& operator<<(std::ostream
&os
,
929 RGWFileHandle
const &rgw_fh
)
931 const auto& fhk
= rgw_fh
.get_key();
932 const auto& fh
= const_cast<RGWFileHandle
&>(rgw_fh
).get_fh();
933 os
<< "<RGWFileHandle:";
934 os
<< "addr=" << &rgw_fh
<< ";";
935 switch (fh
->fh_type
) {
936 case RGW_FS_TYPE_DIRECTORY
:
937 os
<< "type=DIRECTORY;";
939 case RGW_FS_TYPE_FILE
:
943 os
<< "type=UNKNOWN;";
946 os
<< "fid=" << fhk
.fh_hk
.bucket
<< ":" << fhk
.fh_hk
.object
<< ";";
947 os
<< "name=" << rgw_fh
.object_name() << ";";
948 os
<< "refcnt=" << rgw_fh
.get_refcnt() << ";";
953 RGWFileHandle::~RGWFileHandle() {
954 /* cond-unref parent */
955 if (parent
&& (! parent
->is_mount())) {
956 /* safe because if parent->unref causes its deletion,
957 * there are a) by refcnt, no other objects/paths pointing
958 * to it and b) by the semantics of valid iteration of
959 * fh_lru (observed, e.g., by cohort_lru<T,...>::drain())
960 * no unsafe iterators reaching it either--n.b., this constraint
961 * is binding oncode which may in future attempt to e.g.,
962 * cause the eviction of objects in LRU order */
963 (void) get_fs()->unref(parent
);
967 void RGWFileHandle::encode_attrs(ceph::buffer::list
& ux_key1
,
968 ceph::buffer::list
& ux_attrs1
)
970 fh_key
fhk(this->fh
.fh_hk
);
971 rgw::encode(fhk
, ux_key1
);
972 rgw::encode(*this, ux_attrs1
);
973 } /* RGWFileHandle::encode_attrs */
975 DecodeAttrsResult
RGWFileHandle::decode_attrs(const ceph::buffer::list
* ux_key1
,
976 const ceph::buffer::list
* ux_attrs1
)
978 DecodeAttrsResult dar
{ false, false };
980 auto bl_iter_key1
= const_cast<buffer::list
*>(ux_key1
)->begin();
981 rgw::decode(fhk
, bl_iter_key1
);
982 if (fhk
.version
>= 2) {
983 assert(this->fh
.fh_hk
== fhk
.fh_hk
);
988 auto bl_iter_unix1
= const_cast<buffer::list
*>(ux_attrs1
)->begin();
989 rgw::decode(*this, bl_iter_unix1
);
990 if (this->state
.version
< 2) {
995 } /* RGWFileHandle::decode_attrs */
997 bool RGWFileHandle::reclaim() {
998 lsubdout(fs
->get_context(), rgw
, 17)
999 << __func__
<< " " << *this
1001 /* in the non-delete case, handle may still be in handle table */
1002 if (fh_hook
.is_linked()) {
1003 /* in this case, we are being called from a context which holds
1004 * the partition lock */
1005 fs
->fh_cache
.remove(fh
.fh_hk
.object
, this, FHCache::FLAG_NONE
);
1008 } /* RGWFileHandle::reclaim */
1010 bool RGWFileHandle::has_children() const
1012 if (unlikely(! is_dir()))
1015 RGWRMdirCheck
req(fs
->get_context(), fs
->get_user(), this);
1016 int rc
= rgwlib
.get_fe()->execute_req(&req
);
1018 return req
.valid
&& req
.has_children
;
1024 std::ostream
& operator<<(std::ostream
&os
,
1025 RGWFileHandle::readdir_offset
const &offset
)
1028 if (unlikely(!! get
<uint64_t*>(&offset
))) {
1029 uint64_t* ioff
= get
<uint64_t*>(offset
);
1033 os
<< get
<const char*>(offset
);
1037 int RGWFileHandle::readdir(rgw_readdir_cb rcb
, void *cb_arg
,
1038 readdir_offset offset
,
1039 bool *eof
, uint32_t flags
)
1041 using event
= RGWLibFS::event
;
1044 struct timespec now
;
1045 CephContext
* cct
= fs
->get_context();
1047 directory
* d
= get
<directory
>(&variant_type
);
1049 (void) clock_gettime(CLOCK_MONOTONIC_COARSE
, &now
); /* !LOCKED */
1050 lock_guard
guard(mtx
);
1051 d
->last_readdir
= now
;
1055 if (likely(!! get
<const char*>(&offset
))) {
1056 initial_off
= ! get
<const char*>(offset
);
1058 initial_off
= (*get
<uint64_t*>(offset
) == 0);
1062 RGWListBucketsRequest
req(cct
, fs
->get_user(), this, rcb
, cb_arg
,
1064 rc
= rgwlib
.get_fe()->execute_req(&req
);
1066 (void) clock_gettime(CLOCK_MONOTONIC_COARSE
, &now
); /* !LOCKED */
1067 lock_guard
guard(mtx
);
1071 inc_nlink(req
.d_count
);
1073 event
ev(event::type::READDIR
, get_key(), state
.atime
);
1074 lock_guard
sguard(fs
->state
.mtx
);
1075 fs
->state
.push_event(ev
);
1078 RGWReaddirRequest
req(cct
, fs
->get_user(), this, rcb
, cb_arg
, offset
);
1079 rc
= rgwlib
.get_fe()->execute_req(&req
);
1081 (void) clock_gettime(CLOCK_MONOTONIC_COARSE
, &now
); /* !LOCKED */
1082 lock_guard
guard(mtx
);
1086 inc_nlink(req
.d_count
);
1088 event
ev(event::type::READDIR
, get_key(), state
.atime
);
1089 lock_guard
sguard(fs
->state
.mtx
);
1090 fs
->state
.push_event(ev
);
1094 lsubdout(fs
->get_context(), rgw
, 15)
1096 << " final link count=" << state
.nlink
1100 } /* RGWFileHandle::readdir */
1102 int RGWFileHandle::write(uint64_t off
, size_t len
, size_t *bytes_written
,
1106 using WriteCompletion
= RGWLibFS::WriteCompletion
;
1108 lock_guard
guard(mtx
);
1112 file
* f
= get
<file
>(&variant_type
);
1117 lsubdout(fs
->get_context(), rgw
, 5)
1119 << " write attempted on deleted object "
1120 << this->object_name()
1122 /* zap write transaction, if any */
1124 delete f
->write_req
;
1125 f
->write_req
= nullptr;
1130 if (! f
->write_req
) {
1131 /* guard--we do not support (e.g., COW-backed) partial writes */
1133 lsubdout(fs
->get_context(), rgw
, 5)
1135 << " " << object_name()
1136 << " non-0 initial write position " << off
1142 std::string object_name
= relative_object_name();
1144 new RGWWriteRequest(fs
->get_context(), fs
->get_user(), this,
1145 bucket_name(), object_name
);
1146 rc
= rgwlib
.get_fe()->start_req(f
->write_req
);
1148 lsubdout(fs
->get_context(), rgw
, 5)
1150 << this->object_name()
1151 << " write start failed " << off
1152 << " (" << rc
<< ")"
1154 /* zap failed write transaction */
1155 delete f
->write_req
;
1156 f
->write_req
= nullptr;
1159 if (stateless_open()) {
1160 /* start write timer */
1161 f
->write_req
->timer_id
=
1162 RGWLibFS::write_timer
.add_event(
1163 std::chrono::seconds(RGWLibFS::write_completion_interval_s
),
1164 WriteCompletion(*this));
1170 if ((static_cast<off_t
>(off
) < f
->write_req
->real_ofs
) &&
1171 ((f
->write_req
->real_ofs
- off
) <= len
)) {
1172 overlap
= f
->write_req
->real_ofs
- off
;
1173 off
= f
->write_req
->real_ofs
;
1174 buffer
= static_cast<char*>(buffer
) + overlap
;
1182 buffer::create_static(len
, static_cast<char*>(buffer
)));
1185 buffer::copy(static_cast<char*>(buffer
), len
));
1188 f
->write_req
->put_data(off
, bl
);
1189 rc
= f
->write_req
->exec_continue();
1192 size_t min_size
= off
+ len
;
1193 if (min_size
> get_size())
1195 if (stateless_open()) {
1196 /* bump write timer */
1197 RGWLibFS::write_timer
.adjust_event(
1198 f
->write_req
->timer_id
, std::chrono::seconds(10));
1201 /* continuation failed (e.g., non-contiguous write position) */
1202 lsubdout(fs
->get_context(), rgw
, 5)
1205 << " failed write at position " << off
1206 << " (fails write transaction) "
1208 /* zap failed write transaction */
1209 delete f
->write_req
;
1210 f
->write_req
= nullptr;
1214 *bytes_written
= (rc
== 0) ? (len
+ overlap
) : 0;
1216 } /* RGWFileHandle::write */
1218 int RGWFileHandle::write_finish(uint32_t flags
)
1220 unique_lock guard
{mtx
, std::defer_lock
};
1223 if (! (flags
& FLAG_LOCKED
)) {
1227 file
* f
= get
<file
>(&variant_type
);
1228 if (f
&& (f
->write_req
)) {
1229 lsubdout(fs
->get_context(), rgw
, 10)
1231 << " finishing write trans on " << object_name()
1233 rc
= rgwlib
.get_fe()->finish_req(f
->write_req
);
1235 rc
= f
->write_req
->get_ret();
1237 delete f
->write_req
;
1238 f
->write_req
= nullptr;
1242 } /* RGWFileHandle::write_finish */
1244 int RGWFileHandle::close()
1246 lock_guard
guard(mtx
);
1248 int rc
= write_finish(FLAG_LOCKED
);
1250 flags
&= ~FLAG_OPEN
;
1251 flags
&= ~FLAG_STATELESS_OPEN
;
1254 } /* RGWFileHandle::close */
1256 RGWFileHandle::file::~file()
1261 void RGWFileHandle::clear_state()
1263 directory
* d
= get
<directory
>(&variant_type
);
1266 d
->last_marker
= rgw_obj_key
{};
1270 void RGWFileHandle::invalidate() {
1271 RGWLibFS
*fs
= get_fs();
1272 if (fs
->invalidate_cb
) {
1273 fs
->invalidate_cb(fs
->invalidate_arg
, get_key().fh_hk
);
1277 int RGWWriteRequest::exec_start() {
1278 struct req_state
* s
= get_state();
1280 auto compression_type
=
1281 get_store()->get_zone_params().get_compression_type(
1282 s
->bucket_info
.placement_rule
);
1284 /* not obviously supportable */
1285 assert(! dlo_manifest
);
1288 perfcounter
->inc(l_rgw_put
);
1291 if (s
->object
.empty()) {
1292 ldout(s
->cct
, 0) << __func__
<< " called on empty object" << dendl
;
1296 op_ret
= get_params();
1300 op_ret
= get_system_versioning_params(s
, &olh_epoch
, &version_id
);
1305 /* user-supplied MD5 check skipped (not supplied) */
1306 /* early quota check skipped--we don't have size yet */
1307 /* skipping user-supplied etag--we might have one in future, but
1308 * like data it and other attrs would arrive after open */
1309 processor
= select_processor(*static_cast<RGWObjectCtx
*>(s
->obj_ctx
),
1311 op_ret
= processor
->prepare(get_store(), NULL
);
1313 ldout(s
->cct
, 20) << "processor->prepare() returned ret=" << op_ret
1319 if (compression_type
!= "none") {
1320 plugin
= Compressor::create(s
->cct
, compression_type
);
1322 ldout(s
->cct
, 1) << "Cannot load plugin for rgw_compression_type "
1323 << compression_type
<< dendl
;
1325 compressor
.emplace(s
->cct
, plugin
, filter
);
1326 filter
= &*compressor
;
1334 int RGWWriteRequest::exec_continue()
1336 struct req_state
* s
= get_state();
1339 /* check guards (e.g., contig write) */
1343 size_t len
= data
.length();
1347 /* XXX we are currently synchronous--supplied data buffers cannot
1348 * be used after the caller returns */
1349 bool need_to_wait
= true;
1350 bufferlist orig_data
;
1355 hash
.Update((const byte
*)data
.c_str(), data
.length());
1356 op_ret
= put_data_and_throttle(filter
, data
, ofs
, need_to_wait
);
1358 if (!need_to_wait
|| op_ret
!= -EEXIST
) {
1359 ldout(s
->cct
, 20) << "processor->thottle_data() returned ret="
1364 ldout(s
->cct
, 5) << "NOTICE: processor->throttle_data() returned -EEXIST, need to restart write" << dendl
;
1366 /* restore original data */
1367 data
.swap(orig_data
);
1369 /* restart processing with different oid suffix */
1370 dispose_processor(processor
);
1371 processor
= select_processor(*static_cast<RGWObjectCtx
*>(s
->obj_ctx
),
1377 gen_rand_alphanumeric(get_store()->ctx(), buf
, sizeof(buf
) - 1);
1378 oid_rand
.append(buf
);
1380 op_ret
= processor
->prepare(get_store(), &oid_rand
);
1382 ldout(s
->cct
, 0) << "ERROR: processor->prepare() returned "
1387 /* restore compression filter, if any */
1389 compressor
.emplace(s
->cct
, plugin
, filter
);
1390 filter
= &*compressor
;
1393 op_ret
= put_data_and_throttle(filter
, data
, ofs
, false);
1398 bytes_written
+= len
;
1402 } /* exec_continue */
1404 int RGWWriteRequest::exec_finish()
1406 buffer::list bl
, aclbl
, ux_key
, ux_attrs
;
1407 map
<string
, string
>::iterator iter
;
1408 char calc_md5
[CEPH_CRYPTO_MD5_DIGESTSIZE
* 2 + 1];
1409 unsigned char m
[CEPH_CRYPTO_MD5_DIGESTSIZE
];
1410 struct req_state
* s
= get_state();
1412 size_t osize
= rgw_fh
->get_size();
1413 struct timespec octime
= rgw_fh
->get_ctime();
1414 struct timespec omtime
= rgw_fh
->get_mtime();
1415 real_time appx_t
= real_clock::now();
1417 s
->obj_size
= bytes_written
;
1418 perfcounter
->inc(l_rgw_put_b
, s
->obj_size
);
1420 op_ret
= get_store()->check_quota(s
->bucket_owner
.get_id(), s
->bucket
,
1421 user_quota
, bucket_quota
, s
->obj_size
);
1426 op_ret
= get_store()->check_bucket_shards(s
->bucket_info
, s
->bucket
,
1434 if (compressor
&& compressor
->is_compressed()) {
1436 RGWCompressionInfo cs_info
;
1437 cs_info
.compression_type
= plugin
->get_type_name();
1438 cs_info
.orig_size
= s
->obj_size
;
1439 cs_info
.blocks
= std::move(compressor
->get_compression_blocks());
1440 ::encode(cs_info
, tmp
);
1441 attrs
[RGW_ATTR_COMPRESSION
] = tmp
;
1442 ldout(s
->cct
, 20) << "storing " << RGW_ATTR_COMPRESSION
1443 << " with type=" << cs_info
.compression_type
1444 << ", orig_size=" << cs_info
.orig_size
1445 << ", blocks=" << cs_info
.blocks
.size() << dendl
;
1448 buf_to_hex(m
, CEPH_CRYPTO_MD5_DIGESTSIZE
, calc_md5
);
1451 bl
.append(etag
.c_str(), etag
.size() + 1);
1452 emplace_attr(RGW_ATTR_ETAG
, std::move(bl
));
1454 policy
.encode(aclbl
);
1455 emplace_attr(RGW_ATTR_ACL
, std::move(aclbl
));
1458 rgw_fh
->set_mtime(real_clock::to_timespec(appx_t
));
1459 rgw_fh
->set_ctime(real_clock::to_timespec(appx_t
));
1460 rgw_fh
->set_size(bytes_written
);
1461 rgw_fh
->encode_attrs(ux_key
, ux_attrs
);
1463 emplace_attr(RGW_ATTR_UNIX_KEY1
, std::move(ux_key
));
1464 emplace_attr(RGW_ATTR_UNIX1
, std::move(ux_attrs
));
1466 for (iter
= s
->generic_attrs
.begin(); iter
!= s
->generic_attrs
.end();
1468 buffer::list
& attrbl
= attrs
[iter
->first
];
1469 const string
& val
= iter
->second
;
1470 attrbl
.append(val
.c_str(), val
.size() + 1);
1473 op_ret
= rgw_get_request_metadata(s
->cct
, s
->info
, attrs
);
1477 encode_delete_at_attr(delete_at
, attrs
);
1479 /* Add a custom metadata to expose the information whether an object
1480 * is an SLO or not. Appending the attribute must be performed AFTER
1481 * processing any input from user in order to prohibit overwriting. */
1482 if (unlikely(!! slo_info
)) {
1483 buffer::list slo_userindicator_bl
;
1484 ::encode("True", slo_userindicator_bl
);
1485 emplace_attr(RGW_ATTR_SLO_UINDICATOR
, std::move(slo_userindicator_bl
));
1488 op_ret
= processor
->complete(s
->obj_size
, etag
, &mtime
, real_time(), attrs
,
1489 (delete_at
? *delete_at
: real_time()),
1490 if_match
, if_nomatch
);
1492 /* revert attr updates */
1493 rgw_fh
->set_mtime(omtime
);
1494 rgw_fh
->set_ctime(octime
);
1495 rgw_fh
->set_size(osize
);
1499 dispose_processor(processor
);
1500 perfcounter
->tinc(l_rgw_put_lat
,
1501 (ceph_clock_now() - s
->time
));
1505 } /* namespace rgw */
1510 void rgwfile_version(int *major
, int *minor
, int *extra
)
1513 *major
= LIBRGW_FILE_VER_MAJOR
;
1515 *minor
= LIBRGW_FILE_VER_MINOR
;
1517 *extra
= LIBRGW_FILE_VER_EXTRA
;
1521 attach rgw namespace
1523 int rgw_mount(librgw_t rgw
, const char *uid
, const char *acc_key
,
1524 const char *sec_key
, struct rgw_fs
**rgw_fs
,
1529 /* stash access data for "mount" */
1530 RGWLibFS
* new_fs
= new RGWLibFS(static_cast<CephContext
*>(rgw
), uid
, acc_key
,
1534 rc
= new_fs
->authorize(rgwlib
.get_store());
1540 /* register fs for shared gc */
1541 rgwlib
.get_fe()->get_process()->register_fs(new_fs
);
1543 struct rgw_fs
*fs
= new_fs
->get_fs();
1546 /* XXX we no longer assume "/" is unique, but we aren't tracking the
1554 int rgw_mount2(librgw_t rgw
, const char *uid
, const char *acc_key
,
1555 const char *sec_key
, const char *root
, struct rgw_fs
**rgw_fs
,
1560 /* stash access data for "mount" */
1561 RGWLibFS
* new_fs
= new RGWLibFS(static_cast<CephContext
*>(rgw
), uid
, acc_key
,
1565 rc
= new_fs
->authorize(rgwlib
.get_store());
1571 /* register fs for shared gc */
1572 rgwlib
.get_fe()->get_process()->register_fs(new_fs
);
1574 struct rgw_fs
*fs
= new_fs
->get_fs();
1577 /* XXX we no longer assume "/" is unique, but we aren't tracking the
1586 register invalidate callbacks
1588 int rgw_register_invalidate(struct rgw_fs
*rgw_fs
, rgw_fh_callback_t cb
,
1589 void *arg
, uint32_t flags
)
1592 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1593 return fs
->register_invalidate(cb
, arg
, flags
);
1597 detach rgw namespace
1599 int rgw_umount(struct rgw_fs
*rgw_fs
, uint32_t flags
)
1601 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1607 get filesystem attributes
1609 int rgw_statfs(struct rgw_fs
*rgw_fs
,
1610 struct rgw_file_handle
*parent_fh
,
1611 struct rgw_statvfs
*vfs_st
, uint32_t flags
)
1613 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1615 /* XXX for now, just publish a huge capacity and
1616 * limited utiliztion */
1617 vfs_st
->f_bsize
= 1024*1024 /* 1M */;
1618 vfs_st
->f_frsize
= 1024; /* minimal allocation unit (who cares) */
1619 vfs_st
->f_blocks
= UINT64_MAX
;
1620 vfs_st
->f_bfree
= UINT64_MAX
;
1621 vfs_st
->f_bavail
= UINT64_MAX
;
1622 vfs_st
->f_files
= 1024; /* object count, do we have an est? */
1623 vfs_st
->f_ffree
= UINT64_MAX
;
1624 vfs_st
->f_fsid
[0] = fs
->get_fsid();
1625 vfs_st
->f_fsid
[1] = fs
->get_fsid();
1627 vfs_st
->f_namemax
= 4096;
1632 generic create -- create an empty regular file
1634 int rgw_create(struct rgw_fs
*rgw_fs
, struct rgw_file_handle
*parent_fh
,
1635 const char *name
, struct stat
*st
, uint32_t mask
,
1636 struct rgw_file_handle
**fh
, uint32_t posix_flags
,
1641 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1642 RGWFileHandle
* parent
= get_rgwfh(parent_fh
);
1645 (parent
->is_root()) ||
1646 (parent
->is_file())) {
1651 MkObjResult fhr
= fs
->create(parent
, name
, st
, mask
, flags
);
1652 RGWFileHandle
*nfh
= get
<0>(fhr
); // nullptr if !success
1655 *fh
= nfh
->get_fh();
1661 create a new directory
1663 int rgw_mkdir(struct rgw_fs
*rgw_fs
,
1664 struct rgw_file_handle
*parent_fh
,
1665 const char *name
, struct stat
*st
, uint32_t mask
,
1666 struct rgw_file_handle
**fh
, uint32_t flags
)
1670 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1671 RGWFileHandle
* parent
= get_rgwfh(parent_fh
);
1678 MkObjResult fhr
= fs
->mkdir(parent
, name
, st
, mask
, flags
);
1679 RGWFileHandle
*nfh
= get
<0>(fhr
); // nullptr if !success
1682 *fh
= nfh
->get_fh();
1690 int rgw_rename(struct rgw_fs
*rgw_fs
,
1691 struct rgw_file_handle
*src
, const char* src_name
,
1692 struct rgw_file_handle
*dst
, const char* dst_name
,
1695 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1697 RGWFileHandle
* src_fh
= get_rgwfh(src
);
1698 RGWFileHandle
* dst_fh
= get_rgwfh(dst
);
1700 return fs
->rename(src_fh
, dst_fh
, src_name
, dst_name
);
1704 remove file or directory
1706 int rgw_unlink(struct rgw_fs
*rgw_fs
, struct rgw_file_handle
*parent_fh
,
1707 const char *name
, uint32_t flags
)
1709 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1710 RGWFileHandle
* parent
= get_rgwfh(parent_fh
);
1712 return fs
->unlink(parent
, name
);
1716 lookup object by name (POSIX style)
1718 int rgw_lookup(struct rgw_fs
*rgw_fs
,
1719 struct rgw_file_handle
*parent_fh
, const char* path
,
1720 struct rgw_file_handle
**fh
, uint32_t flags
)
1722 //CephContext* cct = static_cast<CephContext*>(rgw_fs->rgw);
1723 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1725 RGWFileHandle
* parent
= get_rgwfh(parent_fh
);
1727 (! parent
->is_dir())) {
1732 RGWFileHandle
* rgw_fh
;
1735 if (parent
->is_root()) {
1736 /* special: parent lookup--note lack of ref()! */
1737 if (unlikely((strcmp(path
, "..") == 0) ||
1738 (strcmp(path
, "/") == 0))) {
1741 RGWLibFS::BucketStats bstat
;
1742 fhr
= fs
->stat_bucket(parent
, path
, bstat
, RGWFileHandle::FLAG_NONE
);
1743 rgw_fh
= get
<0>(fhr
);
1748 /* special: after readdir--note extra ref()! */
1749 if (unlikely((strcmp(path
, "..") == 0))) {
1751 lsubdout(fs
->get_context(), rgw
, 17)
1752 << __func__
<< "BANG"<< *rgw_fh
1756 /* lookup in a readdir callback */
1757 enum rgw_fh_type fh_type
= fh_type_of(flags
);
1759 uint32_t sl_flags
= (flags
& RGW_LOOKUP_FLAG_RCB
)
1760 ? RGWFileHandle::FLAG_NONE
1761 : RGWFileHandle::FLAG_EXACT_MATCH
;
1763 fhr
= fs
->stat_leaf(parent
, path
, fh_type
, sl_flags
);
1764 if (! get
<0>(fhr
)) {
1765 if (! (flags
& RGW_LOOKUP_FLAG_CREATE
))
1768 fhr
= fs
->lookup_fh(parent
, path
, RGWFileHandle::FLAG_CREATE
);
1770 rgw_fh
= get
<0>(fhr
);
1774 struct rgw_file_handle
*rfh
= rgw_fh
->get_fh();
1781 lookup object by handle (NFS style)
1783 int rgw_lookup_handle(struct rgw_fs
*rgw_fs
, struct rgw_fh_hk
*fh_hk
,
1784 struct rgw_file_handle
**fh
, uint32_t flags
)
1786 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1788 RGWFileHandle
* rgw_fh
= fs
->lookup_handle(*fh_hk
);
1794 struct rgw_file_handle
*rfh
= rgw_fh
->get_fh();
1801 * release file handle
1803 int rgw_fh_rele(struct rgw_fs
*rgw_fs
, struct rgw_file_handle
*fh
,
1806 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1807 RGWFileHandle
* rgw_fh
= get_rgwfh(fh
);
1809 lsubdout(fs
->get_context(), rgw
, 17)
1810 << __func__
<< " " << *rgw_fh
1818 get unix attributes for object
1820 int rgw_getattr(struct rgw_fs
*rgw_fs
,
1821 struct rgw_file_handle
*fh
, struct stat
*st
, uint32_t flags
)
1823 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1824 RGWFileHandle
* rgw_fh
= get_rgwfh(fh
);
1826 return fs
->getattr(rgw_fh
, st
);
1830 set unix attributes for object
1832 int rgw_setattr(struct rgw_fs
*rgw_fs
,
1833 struct rgw_file_handle
*fh
, struct stat
*st
,
1834 uint32_t mask
, uint32_t flags
)
1836 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1837 RGWFileHandle
* rgw_fh
= get_rgwfh(fh
);
1839 return fs
->setattr(rgw_fh
, st
, mask
, flags
);
1845 int rgw_truncate(struct rgw_fs
*rgw_fs
,
1846 struct rgw_file_handle
*fh
, uint64_t size
, uint32_t flags
)
1854 int rgw_open(struct rgw_fs
*rgw_fs
,
1855 struct rgw_file_handle
*fh
, uint32_t posix_flags
, uint32_t flags
)
1857 RGWFileHandle
* rgw_fh
= get_rgwfh(fh
);
1860 * need to track specific opens--at least read opens and
1861 * a write open; we need to know when a write open is returned,
1862 * that closes a write transaction
1864 * for now, we will support single-open only, it's preferable to
1865 * anything we can otherwise do without access to the NFS state
1867 if (! rgw_fh
->is_file())
1870 return rgw_fh
->open(flags
);
1876 int rgw_close(struct rgw_fs
*rgw_fs
,
1877 struct rgw_file_handle
*fh
, uint32_t flags
)
1879 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1880 RGWFileHandle
* rgw_fh
= get_rgwfh(fh
);
1881 int rc
= rgw_fh
->close(/* XXX */);
1883 if (flags
& RGW_CLOSE_FLAG_RELE
)
1889 int rgw_readdir(struct rgw_fs
*rgw_fs
,
1890 struct rgw_file_handle
*parent_fh
, uint64_t *offset
,
1891 rgw_readdir_cb rcb
, void *cb_arg
, bool *eof
,
1894 RGWFileHandle
* parent
= get_rgwfh(parent_fh
);
1900 lsubdout(parent
->get_fs()->get_context(), rgw
, 15)
1902 << " offset=" << *offset
1905 if ((*offset
== 0) &&
1906 (flags
& RGW_READDIR_FLAG_DOTDOT
)) {
1907 /* send '.' and '..' with their NFS-defined offsets */
1908 rcb(".", cb_arg
, 1, RGW_LOOKUP_FLAG_DIR
);
1909 rcb("..", cb_arg
, 2, RGW_LOOKUP_FLAG_DIR
);
1912 int rc
= parent
->readdir(rcb
, cb_arg
, offset
, eof
, flags
);
1916 /* enumeration continuing from name */
1917 int rgw_readdir2(struct rgw_fs
*rgw_fs
,
1918 struct rgw_file_handle
*parent_fh
, const char *name
,
1919 rgw_readdir_cb rcb
, void *cb_arg
, bool *eof
,
1922 RGWFileHandle
* parent
= get_rgwfh(parent_fh
);
1928 lsubdout(parent
->get_fs()->get_context(), rgw
, 15)
1930 << " offset=" << name
1934 (flags
& RGW_READDIR_FLAG_DOTDOT
)) {
1935 /* send '.' and '..' with their NFS-defined offsets */
1936 rcb(".", cb_arg
, 1, RGW_LOOKUP_FLAG_DIR
);
1937 rcb("..", cb_arg
, 2, RGW_LOOKUP_FLAG_DIR
);
1940 int rc
= parent
->readdir(rcb
, cb_arg
, name
, eof
, flags
);
1942 } /* rgw_readdir2 */
1944 /* project offset of dirent name */
1945 int rgw_dirent_offset(struct rgw_fs
*rgw_fs
,
1946 struct rgw_file_handle
*parent_fh
,
1947 const char *name
, int64_t *offset
,
1950 RGWFileHandle
* parent
= get_rgwfh(parent_fh
);
1955 std::string sname
{name
};
1956 int rc
= parent
->offset_of(sname
, offset
, flags
);
1963 int rgw_read(struct rgw_fs
*rgw_fs
,
1964 struct rgw_file_handle
*fh
, uint64_t offset
,
1965 size_t length
, size_t *bytes_read
, void *buffer
,
1968 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
1969 RGWFileHandle
* rgw_fh
= get_rgwfh(fh
);
1971 return fs
->read(rgw_fh
, offset
, length
, bytes_read
, buffer
, flags
);
1977 int rgw_write(struct rgw_fs
*rgw_fs
,
1978 struct rgw_file_handle
*fh
, uint64_t offset
,
1979 size_t length
, size_t *bytes_written
, void *buffer
,
1982 RGWFileHandle
* rgw_fh
= get_rgwfh(fh
);
1987 if (! rgw_fh
->is_file())
1990 if (! rgw_fh
->is_open()) {
1991 if (flags
& RGW_OPEN_FLAG_V3
) {
1992 rc
= rgw_fh
->open(flags
);
1999 rc
= rgw_fh
->write(offset
, length
, bytes_written
, buffer
);
2005 read data from file (vector)
2010 struct rgw_vio
* vio
;
2013 RGWReadV(buffer::list
& _bl
, rgw_vio
* _vio
) : vio(_vio
) {
2017 struct rgw_vio
* get_vio() { return vio
; }
2019 const std::list
<buffer::ptr
>& buffers() { return bl
.buffers(); }
2021 unsigned /* XXX */ length() { return bl
.length(); }
2025 void rgw_readv_rele(struct rgw_uio
*uio
, uint32_t flags
)
2027 RGWReadV
* rdv
= static_cast<RGWReadV
*>(uio
->uio_p1
);
2029 ::operator delete(rdv
);
2032 int rgw_readv(struct rgw_fs
*rgw_fs
,
2033 struct rgw_file_handle
*fh
, rgw_uio
*uio
, uint32_t flags
)
2036 CephContext
* cct
= static_cast<CephContext
*>(rgw_fs
->rgw
);
2037 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
2038 RGWFileHandle
* rgw_fh
= get_rgwfh(fh
);
2040 if (! rgw_fh
->is_file())
2046 RGWGetObjRequest
req(cct
, fs
->get_user(), rgw_fh
->bucket_name(),
2047 rgw_fh
->object_name(), uio
->uio_offset
, uio
->uio_resid
,
2049 req
.do_hexdump
= false;
2051 rc
= rgwlib
.get_fe()->execute_req(&req
);
2054 RGWReadV
* rdv
= static_cast<RGWReadV
*>(
2055 ::operator new(sizeof(RGWReadV
) +
2056 (bl
.buffers().size() * sizeof(struct rgw_vio
))));
2059 RGWReadV(bl
, reinterpret_cast<rgw_vio
*>(rdv
+sizeof(RGWReadV
)));
2062 uio
->uio_cnt
= rdv
->buffers().size();
2063 uio
->uio_resid
= rdv
->length();
2064 uio
->uio_vio
= rdv
->get_vio();
2065 uio
->uio_rele
= rgw_readv_rele
;
2068 auto& buffers
= rdv
->buffers();
2069 for (auto& bp
: buffers
) {
2070 rgw_vio
*vio
= &(uio
->uio_vio
[ix
]);
2071 vio
->vio_base
= const_cast<char*>(bp
.c_str());
2072 vio
->vio_len
= bp
.length();
2073 vio
->vio_u1
= nullptr;
2074 vio
->vio_p1
= nullptr;
2086 write data to file (vector)
2088 int rgw_writev(struct rgw_fs
*rgw_fs
, struct rgw_file_handle
*fh
,
2089 rgw_uio
*uio
, uint32_t flags
)
2094 CephContext
* cct
= static_cast<CephContext
*>(rgw_fs
->rgw
);
2095 RGWLibFS
*fs
= static_cast<RGWLibFS
*>(rgw_fs
->fs_private
);
2096 RGWFileHandle
* rgw_fh
= get_rgwfh(fh
);
2098 if (! rgw_fh
->is_file())
2102 for (unsigned int ix
= 0; ix
< uio
->uio_cnt
; ++ix
) {
2103 rgw_vio
*vio
= &(uio
->uio_vio
[ix
]);
2105 buffer::create_static(vio
->vio_len
,
2106 static_cast<char*>(vio
->vio_base
)));
2109 std::string oname
= rgw_fh
->relative_object_name();
2110 RGWPutObjRequest
req(cct
, fs
->get_user(), rgw_fh
->bucket_name(),
2113 int rc
= rgwlib
.get_fe()->execute_req(&req
);
2115 /* XXX update size (in request) */
2123 int rgw_fsync(struct rgw_fs
*rgw_fs
, struct rgw_file_handle
*handle
,
2129 int rgw_commit(struct rgw_fs
*rgw_fs
, struct rgw_file_handle
*fh
,
2130 uint64_t offset
, uint64_t length
, uint32_t flags
)
2132 RGWFileHandle
* rgw_fh
= get_rgwfh(fh
);
2134 return rgw_fh
->commit(offset
, length
, RGWFileHandle::FLAG_NONE
);