]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_file.cc
update sources to v12.1.2
[ceph.git] / ceph / src / rgw / rgw_file.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "include/compat.h"
5 #include "include/rados/rgw_file.h"
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9
10 #include "rgw_lib.h"
11 #include "rgw_rados.h"
12 #include "rgw_resolve.h"
13 #include "rgw_op.h"
14 #include "rgw_rest.h"
15 #include "rgw_acl.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"
24 #include "rgw_user.h"
25 #include "rgw_bucket.h"
26
27 #include "rgw_file.h"
28 #include "rgw_lib_frontend.h"
29
30 #include <atomic>
31
32 #define dout_subsys ceph_subsys_rgw
33
34 using namespace rgw;
35
36 namespace rgw {
37
38 extern RGWLib rgwlib;
39
40 const string RGWFileHandle::root_name = "/";
41
42 std::atomic<uint32_t> RGWLibFS::fs_inst_counter;
43
44 uint32_t RGWLibFS::write_completion_interval_s = 10;
45
46 ceph::timer<ceph::mono_clock> RGWLibFS::write_timer{
47 ceph::construct_suspended};
48
49 inline int valid_fs_bucket_name(const string& name) {
50 int rc = valid_s3_bucket_name(name, false /* relaxed */);
51 if (rc != 0) {
52 if (name.size() > 255)
53 return -ENAMETOOLONG;
54 return -EINVAL;
55 }
56 return 0;
57 }
58
59 inline int valid_fs_object_name(const string& name) {
60 int rc = valid_s3_object_name(name);
61 if (rc != 0) {
62 if (name.size() > 1024)
63 return -ENAMETOOLONG;
64 return -EINVAL;
65 }
66 return 0;
67 }
68
69 LookupFHResult RGWLibFS::stat_bucket(RGWFileHandle* parent, const char *path,
70 RGWLibFS::BucketStats& bs,
71 uint32_t flags)
72 {
73 LookupFHResult fhr{nullptr, 0};
74 std::string bucket_name{path};
75 RGWStatBucketRequest req(cct, get_user(), bucket_name, bs);
76
77 int rc = rgwlib.get_fe()->execute_req(&req);
78 if ((rc == 0) &&
79 (req.get_ret() == 0) &&
80 (req.matched())) {
81 fhr = lookup_fh(parent, path,
82 (flags & RGWFileHandle::FLAG_LOCKED)|
83 RGWFileHandle::FLAG_CREATE|
84 RGWFileHandle::FLAG_BUCKET);
85 if (get<0>(fhr)) {
86 RGWFileHandle* rgw_fh = get<0>(fhr);
87 if (! (flags & RGWFileHandle::FLAG_LOCKED)) {
88 rgw_fh->mtx.lock();
89 }
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 bool old_key = rgw_fh->decode_attrs(ux_key, ux_attrs);
96 if (old_key) {
97 update_fhk(rgw_fh);
98 }
99 }
100 if (! (flags & RGWFileHandle::FLAG_LOCKED)) {
101 rgw_fh->mtx.unlock();
102 }
103 }
104 }
105 return fhr;
106 }
107
108 LookupFHResult RGWLibFS::stat_leaf(RGWFileHandle* parent,
109 const char *path,
110 enum rgw_fh_type type,
111 uint32_t flags)
112 {
113 /* find either-of <object_name>, <object_name/>, only one of
114 * which should exist; atomicity? */
115 using std::get;
116
117 LookupFHResult fhr{nullptr, 0};
118
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 */
123
124 std::string obj_path = parent->format_child_name(path, false);
125
126 for (auto ix : { 0, 1, 2 }) {
127 switch (ix) {
128 case 0:
129 {
130 /* type hint */
131 if (type == RGW_FS_TYPE_DIRECTORY)
132 continue;
133
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);
138 if ((rc == 0) &&
139 (req.get_ret() == 0)) {
140 fhr = lookup_fh(parent, path, RGWFileHandle::FLAG_CREATE);
141 if (get<0>(fhr)) {
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 bool old_key = rgw_fh->decode_attrs(ux_key, ux_attrs);
151 if (old_key) {
152 update_fhk(rgw_fh);
153 }
154 }
155 }
156 goto done;
157 }
158 }
159 break;
160 case 1:
161 {
162 /* try dir form */
163 /* type hint */
164 if (type == RGW_FS_TYPE_FILE)
165 continue;
166
167 obj_path += "/";
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);
172 if ((rc == 0) &&
173 (req.get_ret() == 0)) {
174 fhr = lookup_fh(parent, path, RGWFileHandle::FLAG_DIRECTORY);
175 if (get<0>(fhr)) {
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 bool old_key = rgw_fh->decode_attrs(ux_key, ux_attrs);
185 if (old_key) {
186 update_fhk(rgw_fh);
187 }
188 }
189 }
190 goto done;
191 }
192 }
193 break;
194 case 2:
195 {
196 std::string object_name{path};
197 RGWStatLeafRequest req(cct, get_user(), parent, object_name);
198 int rc = rgwlib.get_fe()->execute_req(&req);
199 if ((rc == 0) &&
200 (req.get_ret() == 0)) {
201 if (req.matched) {
202 /* we need rgw object's key name equal to file name, if
203 * not return NULL */
204 if ((flags & RGWFileHandle::FLAG_EXACT_MATCH) &&
205 !req.exact_matched) {
206 lsubdout(get_context(), rgw, 15)
207 << __func__
208 << ": stat leaf not exact match file name = "
209 << path << dendl;
210 goto done;
211 }
212 fhr = lookup_fh(parent, path,
213 RGWFileHandle::FLAG_CREATE|
214 ((req.is_dir) ?
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 */
221 if (get<0>(fhr)) {
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());
226 }
227 }
228 }
229 }
230 break;
231 default:
232 /* not reached */
233 break;
234 }
235 }
236 done:
237 return fhr;
238 } /* RGWLibFS::stat_leaf */
239
240 int RGWLibFS::read(RGWFileHandle* rgw_fh, uint64_t offset, size_t length,
241 size_t* bytes_read, void* buffer, uint32_t flags)
242 {
243 if (! rgw_fh->is_file())
244 return -EINVAL;
245
246 if (rgw_fh->deleted())
247 return -ESTALE;
248
249 RGWReadRequest req(get_context(), get_user(), rgw_fh, offset, length,
250 buffer);
251
252 int rc = rgwlib.get_fe()->execute_req(&req);
253 if ((rc == 0) &&
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;
258 }
259
260 return rc;
261 }
262
263 int RGWLibFS::unlink(RGWFileHandle* rgw_fh, const char* name, uint32_t flags)
264 {
265 int rc = 0;
266 BucketStats bs;
267 RGWFileHandle* parent = nullptr;
268 RGWFileHandle* bkt_fh = nullptr;
269
270 if (unlikely(flags & RGWFileHandle::FLAG_UNLINK_THIS)) {
271 /* LOCKED */
272 parent = rgw_fh->get_parent();
273 } else {
274 /* atomicity */
275 parent = rgw_fh;
276 LookupFHResult fhr = lookup_fh(parent, name, RGWFileHandle::FLAG_LOCK);
277 rgw_fh = get<0>(fhr);
278 /* LOCKED */
279 }
280
281 if (parent->is_root()) {
282 /* a bucket may have an object storing Unix attributes, check
283 * for and delete it */
284 LookupFHResult fhr;
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 */
291 return -ENOENT;
292 }
293
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
298 * special case of
299 * RGWFileHandle::FLAG_UNLINK_THIS) */
300 rgw_fh->mtx.unlock();
301 unref(rgw_fh);
302 }
303 return -ENOTEMPTY;
304 } else {
305 /* delete object w/key "<bucket>/" (uxattrs), if any */
306 string oname{"/"};
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 */
310 unref(bkt_fh);
311 }
312
313 string bname{name};
314 RGWDeleteBucketRequest req(cct, get_user(), bname);
315 rc = rgwlib.get_fe()->execute_req(&req);
316 if (! rc) {
317 rc = req.get_ret();
318 }
319 } else {
320 /*
321 * leaf object
322 */
323 if (! rgw_fh) {
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);
330 if (!! rc)
331 return rc;
332
333 /* rgw_fh ref+ */
334 rgw_fh = get_rgwfh(fh);
335 rgw_fh->mtx.lock(); /* LOCKED */
336 }
337
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
341 * child cache */
342 if (rgw_fh->has_children()) {
343 rgw_fh->mtx.unlock();
344 unref(rgw_fh);
345 return(-ENOTEMPTY);
346 }
347 oname += "/";
348 }
349 RGWDeleteObjRequest req(cct, get_user(), parent->bucket_name(),
350 oname);
351 rc = rgwlib.get_fe()->execute_req(&req);
352 if (! rc) {
353 rc = req.get_ret();
354 }
355 }
356
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);
362 }
363
364 if (! rc) {
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));
368 }
369
370 rgw_fh->mtx.unlock();
371 unref(rgw_fh);
372
373 return rc;
374 } /* RGWLibFS::unlink */
375
376 int RGWLibFS::rename(RGWFileHandle* src_fh, RGWFileHandle* dst_fh,
377 const char *_src_name, const char *_dst_name)
378
379 {
380 /* XXX initial implementation: try-copy, and delete if copy
381 * succeeds */
382 int rc = -EINVAL;
383
384 real_time t;
385
386 std::string src_name{_src_name};
387 std::string dst_name{_dst_name};
388
389 /* atomicity */
390 LookupFHResult fhr = lookup_fh(src_fh, _src_name, RGWFileHandle::FLAG_LOCK);
391 RGWFileHandle* rgw_fh = get<0>(fhr);
392
393 /* should not happen */
394 if (! rgw_fh) {
395 ldout(get_context(), 0) << __func__
396 << " BUG no such src renaming path="
397 << src_name
398 << dendl;
399 goto out;
400 }
401
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()
407 << dendl;
408 rc = -EPERM;
409 goto unlock;
410 }
411
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()
417 << dendl;
418 rc = -EPERM;
419 goto unlock;
420 }
421
422 t = real_clock::now();
423
424 for (int ix : {0, 1}) {
425 switch (ix) {
426 case 0:
427 {
428 RGWCopyObjRequest req(cct, get_user(), src_fh, dst_fh, src_name,
429 dst_name);
430 int rc = rgwlib.get_fe()->execute_req(&req);
431 if ((rc != 0) ||
432 ((rc = req.get_ret()) != 0)) {
433 ldout(get_context(), 1)
434 << __func__
435 << " rename step 0 failed src="
436 << src_fh->full_object_name() << " " << src_name
437 << " dst=" << dst_fh->full_object_name()
438 << " " << dst_name
439 << "rc " << rc
440 << dendl;
441 goto unlock;
442 }
443 ldout(get_context(), 12)
444 << __func__
445 << " rename step 0 success src="
446 << src_fh->full_object_name() << " " << src_name
447 << " dst=" << dst_fh->full_object_name()
448 << " " << dst_name
449 << " rc " << rc
450 << dendl;
451 /* update dst change id */
452 dst_fh->set_times(t);
453 }
454 break;
455 case 1:
456 {
457 rc = this->unlink(rgw_fh /* LOCKED */, _src_name,
458 RGWFileHandle::FLAG_UNLINK_THIS);
459 /* !LOCKED, -ref */
460 if (! rc) {
461 ldout(get_context(), 12)
462 << __func__
463 << " rename step 1 success src="
464 << src_fh->full_object_name() << " " << src_name
465 << " dst=" << dst_fh->full_object_name()
466 << " " << dst_name
467 << " rc " << rc
468 << dendl;
469 /* update src change id */
470 src_fh->set_times(t);
471 } else {
472 ldout(get_context(), 1)
473 << __func__
474 << " rename step 1 failed src="
475 << src_fh->full_object_name() << " " << src_name
476 << " dst=" << dst_fh->full_object_name()
477 << " " << dst_name
478 << " rc " << rc
479 << dendl;
480 }
481 }
482 goto out;
483 default:
484 abort();
485 } /* switch */
486 } /* ix */
487 unlock:
488 rgw_fh->mtx.unlock(); /* !LOCKED */
489 unref(rgw_fh); /* -ref */
490
491 out:
492 return rc;
493 } /* RGWLibFS::rename */
494
495 MkObjResult RGWLibFS::mkdir(RGWFileHandle* parent, const char *name,
496 struct stat *st, uint32_t mask, uint32_t flags)
497 {
498 int rc, rc2;
499 rgw_file_handle *lfh;
500
501 rc = rgw_lookup(get_fs(), parent->get_fh(), name, &lfh,
502 RGW_LOOKUP_FLAG_NONE);
503 if (! rc) {
504 /* conflict! */
505 rc = rgw_fh_rele(get_fs(), lfh, RGW_FH_RELE_FLAG_NONE);
506 return MkObjResult{nullptr, -EEXIST};
507 }
508
509 MkObjResult mkr{nullptr, -EINVAL};
510 LookupFHResult fhr;
511 RGWFileHandle* rgw_fh = nullptr;
512 buffer::list ux_key, ux_attrs;
513
514 fhr = lookup_fh(parent, name,
515 RGWFileHandle::FLAG_CREATE|
516 RGWFileHandle::FLAG_DIRECTORY|
517 RGWFileHandle::FLAG_LOCK);
518 rgw_fh = get<0>(fhr);
519 if (rgw_fh) {
520 rgw_fh->create_stat(st, mask);
521 rgw_fh->set_times(real_clock::now());
522 /* save attrs */
523 rgw_fh->encode_attrs(ux_key, ux_attrs);
524 if (st)
525 rgw_fh->stat(st);
526 get<0>(mkr) = rgw_fh;
527 } else {
528 get<1>(mkr) = -EIO;
529 return mkr;
530 }
531
532 if (parent->is_root()) {
533 /* bucket */
534 string bname{name};
535 /* enforce S3 name restrictions */
536 rc = valid_fs_bucket_name(bname);
537 if (rc != 0) {
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();
542 unref(rgw_fh);
543 get<0>(mkr) = nullptr;
544 get<1>(mkr) = rc;
545 return mkr;
546 }
547
548 RGWCreateBucketRequest req(get_context(), get_user(), bname);
549
550 /* save attrs */
551 req.emplace_attr(RGW_ATTR_UNIX_KEY1, std::move(ux_key));
552 req.emplace_attr(RGW_ATTR_UNIX1, std::move(ux_attrs));
553
554 rc = rgwlib.get_fe()->execute_req(&req);
555 rc2 = req.get_ret();
556 } else {
557 /* create an object representing the directory */
558 buffer::list bl;
559 string dir_name = parent->format_child_name(name, true);
560
561 /* need valid S3 name (characters, length <= 1024, etc) */
562 rc = valid_fs_object_name(dir_name);
563 if (rc != 0) {
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();
568 unref(rgw_fh);
569 get<0>(mkr) = nullptr;
570 get<1>(mkr) = rc;
571 return mkr;
572 }
573
574 RGWPutObjRequest req(get_context(), get_user(), parent->bucket_name(),
575 dir_name, bl);
576
577 /* save attrs */
578 req.emplace_attr(RGW_ATTR_UNIX_KEY1, std::move(ux_key));
579 req.emplace_attr(RGW_ATTR_UNIX1, std::move(ux_attrs));
580
581 rc = rgwlib.get_fe()->execute_req(&req);
582 rc2 = req.get_ret();
583 }
584
585 if (! ((rc == 0) &&
586 (rc2 == 0))) {
587 /* op failed */
588 rgw_fh->flags |= RGWFileHandle::FLAG_DELETED;
589 rgw_fh->mtx.unlock(); /* !LOCKED */
590 unref(rgw_fh);
591 get<0>(mkr) = nullptr;
592 /* fixup rc */
593 if (!rc)
594 rc = rc2;
595 } else {
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 */
600 }
601
602 get<1>(mkr) = rc;
603
604 return mkr;
605 } /* RGWLibFS::mkdir */
606
607 MkObjResult RGWLibFS::create(RGWFileHandle* parent, const char *name,
608 struct stat *st, uint32_t mask, uint32_t flags)
609 {
610 int rc, rc2;
611
612 using std::get;
613
614 rgw_file_handle *lfh;
615 rc = rgw_lookup(get_fs(), parent->get_fh(), name, &lfh,
616 RGW_LOOKUP_FLAG_NONE);
617 if (! rc) {
618 /* conflict! */
619 rc = rgw_fh_rele(get_fs(), lfh, RGW_FH_RELE_FLAG_NONE);
620 return MkObjResult{nullptr, -EEXIST};
621 }
622
623 /* expand and check name */
624 std::string obj_name = parent->format_child_name(name, false);
625 rc = valid_fs_object_name(obj_name);
626 if (rc != 0) {
627 return MkObjResult{nullptr, rc};
628 }
629
630 /* create it */
631 buffer::list bl;
632 RGWPutObjRequest req(cct, get_user(), parent->bucket_name(), obj_name, bl);
633 MkObjResult mkr{nullptr, -EINVAL};
634
635 rc = rgwlib.get_fe()->execute_req(&req);
636 rc2 = req.get_ret();
637
638 if ((rc == 0) &&
639 (rc2 == 0)) {
640 /* XXX atomicity */
641 LookupFHResult fhr = lookup_fh(parent, name, RGWFileHandle::FLAG_CREATE |
642 RGWFileHandle::FLAG_LOCK);
643 RGWFileHandle* rgw_fh = get<0>(fhr);
644 if (rgw_fh) {
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);
650
651 parent->set_mtime(real_clock::to_timespec(t));
652 parent->set_ctime(real_clock::to_timespec(t));
653 }
654 if (st)
655 (void) rgw_fh->stat(st);
656 get<0>(mkr) = rgw_fh;
657 rgw_fh->mtx.unlock();
658 } else
659 rc = -EIO;
660 }
661
662 get<1>(mkr) = rc;
663
664 return mkr;
665 } /* RGWLibFS::create */
666
667 int RGWLibFS::getattr(RGWFileHandle* rgw_fh, struct stat* st)
668 {
669 switch(rgw_fh->fh.fh_type) {
670 case RGW_FS_TYPE_FILE:
671 {
672 if (rgw_fh->deleted())
673 return -ESTALE;
674 }
675 break;
676 default:
677 break;
678 };
679
680 return rgw_fh->stat(st);
681 } /* RGWLibFS::getattr */
682
683 int RGWLibFS::setattr(RGWFileHandle* rgw_fh, struct stat* st, uint32_t mask,
684 uint32_t flags)
685 {
686 int rc, rc2;
687 buffer::list ux_key, ux_attrs;
688
689 lock_guard guard(rgw_fh->mtx);
690
691 switch(rgw_fh->fh.fh_type) {
692 case RGW_FS_TYPE_FILE:
693 {
694 if (rgw_fh->deleted())
695 return -ESTALE;
696 }
697 break;
698 default:
699 break;
700 };
701
702 string obj_name{rgw_fh->relative_object_name()};
703
704 if (rgw_fh->is_dir() &&
705 (likely(! rgw_fh->is_bucket()))) {
706 obj_name += "/";
707 }
708
709 RGWSetAttrsRequest req(cct, get_user(), rgw_fh->bucket_name(), obj_name);
710
711 rgw_fh->create_stat(st, mask);
712 rgw_fh->encode_attrs(ux_key, ux_attrs);
713
714 /* save attrs */
715 req.emplace_attr(RGW_ATTR_UNIX_KEY1, std::move(ux_key));
716 req.emplace_attr(RGW_ATTR_UNIX1, std::move(ux_attrs));
717
718 rc = rgwlib.get_fe()->execute_req(&req);
719 rc2 = req.get_ret();
720
721 if (rc == -ENOENT) {
722 /* special case: materialize placeholder dir */
723 buffer::list bl;
724 RGWPutObjRequest req(get_context(), get_user(), rgw_fh->bucket_name(),
725 obj_name, bl);
726
727 rgw_fh->encode_attrs(ux_key, ux_attrs); /* because std::moved */
728
729 /* save attrs */
730 req.emplace_attr(RGW_ATTR_UNIX_KEY1, std::move(ux_key));
731 req.emplace_attr(RGW_ATTR_UNIX1, std::move(ux_attrs));
732
733 rc = rgwlib.get_fe()->execute_req(&req);
734 rc2 = req.get_ret();
735 }
736
737 if ((rc != 0) || (rc2 != 0)) {
738 return -EIO;
739 }
740
741 rgw_fh->set_ctime(real_clock::to_timespec(real_clock::now()));
742
743 return 0;
744 } /* RGWLibFS::setattr */
745
746 /* called under rgw_fh->mtx held */
747 void RGWLibFS::update_fhk(RGWFileHandle *rgw_fh)
748 {
749 int rc, rc2;
750 string obj_name{rgw_fh->relative_object_name()};
751 buffer::list ux_key, ux_attrs;
752
753 if (rgw_fh->is_dir() &&
754 (likely(! rgw_fh->is_bucket()))) {
755 obj_name += "/";
756 }
757
758 lsubdout(get_context(), rgw, 17)
759 << __func__
760 << " update old versioned fhk : " << obj_name
761 << dendl;
762
763 RGWSetAttrsRequest req(cct, get_user(), rgw_fh->bucket_name(), obj_name);
764
765 rgw_fh->encode_attrs(ux_key, ux_attrs);
766
767 /* update ux_key only */
768 req.emplace_attr(RGW_ATTR_UNIX_KEY1, std::move(ux_key));
769
770 rc = rgwlib.get_fe()->execute_req(&req);
771 rc2 = req.get_ret();
772
773 if ((rc != 0) || (rc2 != 0)) {
774 lsubdout(get_context(), rgw, 17)
775 << __func__
776 << " update fhk failed : " << obj_name
777 << dendl;
778 }
779 } /* RGWLibFS::update_fhk */
780
781 void RGWLibFS::close()
782 {
783 state.flags |= FLAG_CLOSED;
784
785 class ObjUnref
786 {
787 RGWLibFS* fs;
788 public:
789 ObjUnref(RGWLibFS* fs) : fs(fs) {}
790 void operator()(RGWFileHandle* fh) const {
791 lsubdout(fs->get_context(), rgw, 5)
792 << __func__
793 << fh->name
794 << " before ObjUnref refs=" << fh->get_refcnt()
795 << dendl;
796 fs->unref(fh);
797 }
798 };
799
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);
804 rele();
805 } /* RGWLibFS::close */
806
807 inline std::ostream& operator<<(std::ostream &os, struct timespec const &ts) {
808 os << "<timespec: tv_sec=";
809 os << ts.tv_sec;
810 os << "; tv_nsec=";
811 os << ts.tv_nsec;
812 os << ">";
813 return os;
814 }
815
816 std::ostream& operator<<(std::ostream &os, RGWLibFS::event const &ev) {
817 os << "<event:";
818 switch (ev.t) {
819 case RGWLibFS::event::type::READDIR:
820 os << "type=READDIR;";
821 break;
822 default:
823 os << "type=UNKNOWN;";
824 break;
825 };
826 os << "fid=" << ev.fhk.fh_hk.bucket << ":" << ev.fhk.fh_hk.object
827 << ";ts=" << ev.ts << ">";
828 return os;
829 }
830
831 void RGWLibFS::gc()
832 {
833 using std::get;
834 using directory = RGWFileHandle::directory;
835
836 /* dirent invalidate timeout--basically, the upper-bound on
837 * inconsistency with the S3 namespace */
838 auto expire_s
839 = get_context()->_conf->rgw_nfs_namespace_expire_secs;
840
841 /* max events to gc in one cycle */
842 uint32_t max_ev = get_context()->_conf->rgw_nfs_max_gc;
843
844 struct timespec now, expire_ts;
845 event_vector ve;
846 bool stop = false;
847 std::deque<event> &events = state.events;
848
849 do {
850 (void) clock_gettime(CLOCK_MONOTONIC_COARSE, &now);
851 lsubdout(get_context(), rgw, 15)
852 << "GC: top of expire loop"
853 << " now=" << now
854 << " expire_s=" << expire_s
855 << dendl;
856 {
857 lock_guard guard(state.mtx); /* LOCKED */
858 /* just return if no events */
859 if (events.empty()) {
860 return;
861 }
862 uint32_t _max_ev =
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();
866 expire_ts = ev.ts;
867 expire_ts.tv_sec += expire_s;
868 if (expire_ts > now) {
869 stop = true;
870 break;
871 }
872 ve.push_back(ev);
873 events.pop_front();
874 }
875 } /* anon */
876 /* !LOCKED */
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;
884 if (rgw_fh) {
885 RGWFileHandle::directory* d;
886 if (unlikely(! rgw_fh->is_dir())) {
887 lsubdout(get_context(), rgw, 0)
888 << __func__
889 << " BUG non-directory found with READDIR event "
890 << "(" << rgw_fh->bucket_name() << ","
891 << rgw_fh->object_name() << ")"
892 << dendl;
893 goto rele;
894 }
895 /* maybe clear state */
896 d = get<directory>(&rgw_fh->variant_type);
897 if (d) {
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
908 << dendl;
909 continue;
910 } else {
911 lsubdout(get_context(), rgw, 15)
912 << "GC: expiring "
913 << rgw_fh->object_name()
914 << dendl;
915 rgw_fh->clear_state();
916 rgw_fh->invalidate();
917 }
918 }
919 rele:
920 unref(rgw_fh);
921 } /* rgw_fh */
922 } /* event::type::READDIR */
923 } /* ev */
924 ve.clear();
925 } while (! (stop || shutdown));
926 } /* RGWLibFS::gc */
927
928 std::ostream& operator<<(std::ostream &os,
929 RGWFileHandle const &rgw_fh)
930 {
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;";
938 break;
939 case RGW_FS_TYPE_FILE:
940 os << "type=FILE;";
941 break;
942 default:
943 os << "type=UNKNOWN;";
944 break;
945 };
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() << ";";
949 os << ">";
950 return os;
951 }
952
953 RGWFileHandle::~RGWFileHandle() {
954 /* in the non-delete case, handle may still be in handle table */
955 if (fh_hook.is_linked()) {
956 fs->fh_cache.remove(fh.fh_hk.object, this, FHCache::FLAG_LOCK);
957 }
958 /* cond-unref parent */
959 if (parent && (! parent->is_root())) {
960 /* safe because if parent->unref causes its deletion,
961 * there are a) by refcnt, no other objects/paths pointing
962 * to it and b) by the semantics of valid iteration of
963 * fh_lru (observed, e.g., by cohort_lru<T,...>::drain())
964 * no unsafe iterators reaching it either--n.b., this constraint
965 * is binding oncode which may in future attempt to e.g.,
966 * cause the eviction of objects in LRU order */
967 (void) get_fs()->unref(parent);
968 }
969 }
970
971 void RGWFileHandle::encode_attrs(ceph::buffer::list& ux_key1,
972 ceph::buffer::list& ux_attrs1)
973 {
974 fh_key fhk(this->fh.fh_hk);
975 rgw::encode(fhk, ux_key1);
976 rgw::encode(*this, ux_attrs1);
977 } /* RGWFileHandle::encode_attrs */
978
979 bool RGWFileHandle::decode_attrs(const ceph::buffer::list* ux_key1,
980 const ceph::buffer::list* ux_attrs1)
981 {
982 bool old_key = false;
983 fh_key fhk;
984 auto bl_iter_key1 = const_cast<buffer::list*>(ux_key1)->begin();
985 rgw::decode(fhk, bl_iter_key1);
986 if (fhk.version >= 2) {
987 assert(this->fh.fh_hk == fhk.fh_hk);
988 } else {
989 old_key = true;
990 }
991
992 auto bl_iter_unix1 = const_cast<buffer::list*>(ux_attrs1)->begin();
993 rgw::decode(*this, bl_iter_unix1);
994
995 return old_key;
996 } /* RGWFileHandle::decode_attrs */
997
998 bool RGWFileHandle::reclaim() {
999 lsubdout(fs->get_context(), rgw, 17)
1000 << __func__ << " " << *this
1001 << dendl;
1002 /* remove if still in fh_cache */
1003 if (fh_hook.is_linked()) {
1004 fs->fh_cache.remove(fh.fh_hk.object, this, FHCache::FLAG_LOCK);
1005 }
1006 return true;
1007 } /* RGWFileHandle::reclaim */
1008
1009 bool RGWFileHandle::has_children() const
1010 {
1011 if (unlikely(! is_dir()))
1012 return false;
1013
1014 RGWRMdirCheck req(fs->get_context(), fs->get_user(), this);
1015 int rc = rgwlib.get_fe()->execute_req(&req);
1016 if (! rc) {
1017 return req.valid && req.has_children;
1018 }
1019
1020 return false;
1021 }
1022
1023 int RGWFileHandle::readdir(rgw_readdir_cb rcb, void *cb_arg, uint64_t *offset,
1024 bool *eof, uint32_t flags)
1025 {
1026 using event = RGWLibFS::event;
1027 int rc = 0;
1028 struct timespec now;
1029 CephContext* cct = fs->get_context();
1030
1031 if ((*offset == 0) &&
1032 (flags & RGW_READDIR_FLAG_DOTDOT)) {
1033 /* send '.' and '..' with their NFS-defined offsets */
1034 rcb(".", cb_arg, 1, RGW_LOOKUP_FLAG_DIR);
1035 rcb("..", cb_arg, 2, RGW_LOOKUP_FLAG_DIR);
1036 }
1037
1038 lsubdout(fs->get_context(), rgw, 15)
1039 << __func__
1040 << " offset=" << *offset
1041 << dendl;
1042
1043 directory* d = get<directory>(&variant_type);
1044 if (d) {
1045 (void) clock_gettime(CLOCK_MONOTONIC_COARSE, &now); /* !LOCKED */
1046 lock_guard guard(mtx);
1047 d->last_readdir = now;
1048 }
1049
1050 if (is_root()) {
1051 RGWListBucketsRequest req(cct, fs->get_user(), this, rcb, cb_arg,
1052 offset);
1053 rc = rgwlib.get_fe()->execute_req(&req);
1054 if (! rc) {
1055 (void) clock_gettime(CLOCK_MONOTONIC_COARSE, &now); /* !LOCKED */
1056 lock_guard guard(mtx);
1057 state.atime = now;
1058 if (*offset == 0)
1059 set_nlink(2);
1060 inc_nlink(req.d_count);
1061 *eof = req.eof();
1062 event ev(event::type::READDIR, get_key(), state.atime);
1063 lock_guard sguard(fs->state.mtx);
1064 fs->state.push_event(ev);
1065 }
1066 } else {
1067 RGWReaddirRequest req(cct, fs->get_user(), this, rcb, cb_arg, offset);
1068 rc = rgwlib.get_fe()->execute_req(&req);
1069 if (! rc) {
1070 (void) clock_gettime(CLOCK_MONOTONIC_COARSE, &now); /* !LOCKED */
1071 lock_guard guard(mtx);
1072 state.atime = now;
1073 if (*offset == 0)
1074 set_nlink(2);
1075 inc_nlink(req.d_count);
1076 *eof = req.eof();
1077 event ev(event::type::READDIR, get_key(), state.atime);
1078 lock_guard sguard(fs->state.mtx);
1079 fs->state.push_event(ev);
1080 }
1081 }
1082
1083 lsubdout(fs->get_context(), rgw, 15)
1084 << __func__
1085 << " final link count=" << state.nlink
1086 << dendl;
1087
1088 return rc;
1089 } /* RGWFileHandle::readdir */
1090
1091 int RGWFileHandle::write(uint64_t off, size_t len, size_t *bytes_written,
1092 void *buffer)
1093 {
1094 using std::get;
1095 using WriteCompletion = RGWLibFS::WriteCompletion;
1096
1097 lock_guard guard(mtx);
1098
1099 int rc = 0;
1100
1101 file* f = get<file>(&variant_type);
1102 if (! f)
1103 return -EISDIR;
1104
1105 if (deleted()) {
1106 lsubdout(fs->get_context(), rgw, 5)
1107 << __func__
1108 << " write attempted on deleted object "
1109 << this->object_name()
1110 << dendl;
1111 /* zap write transaction, if any */
1112 if (f->write_req) {
1113 delete f->write_req;
1114 f->write_req = nullptr;
1115 }
1116 return -ESTALE;
1117 }
1118
1119 if (! f->write_req) {
1120 /* guard--we do not support (e.g., COW-backed) partial writes */
1121 if (off != 0) {
1122 lsubdout(fs->get_context(), rgw, 5)
1123 << __func__
1124 << " " << object_name()
1125 << " non-0 initial write position " << off
1126 << dendl;
1127 return -EIO;
1128 }
1129
1130 /* start */
1131 std::string object_name = relative_object_name();
1132 f->write_req =
1133 new RGWWriteRequest(fs->get_context(), fs->get_user(), this,
1134 bucket_name(), object_name);
1135 rc = rgwlib.get_fe()->start_req(f->write_req);
1136 if (rc < 0) {
1137 lsubdout(fs->get_context(), rgw, 5)
1138 << __func__
1139 << this->object_name()
1140 << " write start failed " << off
1141 << " (" << rc << ")"
1142 << dendl;
1143 /* zap failed write transaction */
1144 delete f->write_req;
1145 f->write_req = nullptr;
1146 return -EIO;
1147 } else {
1148 if (stateless_open()) {
1149 /* start write timer */
1150 f->write_req->timer_id =
1151 RGWLibFS::write_timer.add_event(
1152 std::chrono::seconds(RGWLibFS::write_completion_interval_s),
1153 WriteCompletion(*this));
1154 }
1155 }
1156 }
1157
1158 buffer::list bl;
1159 /* XXXX */
1160 #if 0
1161 bl.push_back(
1162 buffer::create_static(len, static_cast<char*>(buffer)));
1163 #else
1164 bl.push_back(
1165 buffer::copy(static_cast<char*>(buffer), len));
1166 #endif
1167
1168 f->write_req->put_data(off, bl);
1169 rc = f->write_req->exec_continue();
1170
1171 if (rc == 0) {
1172 size_t min_size = off + len;
1173 if (min_size > get_size())
1174 set_size(min_size);
1175 if (stateless_open()) {
1176 /* bump write timer */
1177 RGWLibFS::write_timer.adjust_event(
1178 f->write_req->timer_id, std::chrono::seconds(10));
1179 }
1180 } else {
1181 /* continuation failed (e.g., non-contiguous write position) */
1182 lsubdout(fs->get_context(), rgw, 5)
1183 << __func__
1184 << object_name()
1185 << " failed write at position " << off
1186 << " (fails write transaction) "
1187 << dendl;
1188 /* zap failed write transaction */
1189 delete f->write_req;
1190 f->write_req = nullptr;
1191 rc = -EIO;
1192 }
1193
1194 *bytes_written = (rc == 0) ? len : 0;
1195 return rc;
1196 } /* RGWFileHandle::write */
1197
1198 int RGWFileHandle::write_finish(uint32_t flags)
1199 {
1200 unique_lock guard{mtx, std::defer_lock};
1201 int rc = 0;
1202
1203 if (! (flags & FLAG_LOCKED)) {
1204 guard.lock();
1205 }
1206
1207 file* f = get<file>(&variant_type);
1208 if (f && (f->write_req)) {
1209 lsubdout(fs->get_context(), rgw, 10)
1210 << __func__
1211 << " finishing write trans on " << object_name()
1212 << dendl;
1213 rc = rgwlib.get_fe()->finish_req(f->write_req);
1214 if (! rc) {
1215 rc = f->write_req->get_ret();
1216 }
1217 delete f->write_req;
1218 f->write_req = nullptr;
1219 }
1220
1221 return rc;
1222 } /* RGWFileHandle::write_finish */
1223
1224 int RGWFileHandle::close()
1225 {
1226 lock_guard guard(mtx);
1227
1228 int rc = write_finish(FLAG_LOCKED);
1229
1230 flags &= ~FLAG_OPEN;
1231 flags &= ~FLAG_STATELESS_OPEN;
1232
1233 return rc;
1234 } /* RGWFileHandle::close */
1235
1236 RGWFileHandle::file::~file()
1237 {
1238 delete write_req;
1239 }
1240
1241 void RGWFileHandle::clear_state()
1242 {
1243 directory* d = get<directory>(&variant_type);
1244 if (d) {
1245 state.nlink = 2;
1246 d->last_marker = rgw_obj_key{};
1247 }
1248 }
1249
1250 void RGWFileHandle::invalidate() {
1251 RGWLibFS *fs = get_fs();
1252 if (fs->invalidate_cb) {
1253 fs->invalidate_cb(fs->invalidate_arg, get_key().fh_hk);
1254 }
1255 }
1256
1257 int RGWWriteRequest::exec_start() {
1258 struct req_state* s = get_state();
1259
1260 auto compression_type =
1261 get_store()->get_zone_params().get_compression_type(
1262 s->bucket_info.placement_rule);
1263
1264 /* not obviously supportable */
1265 assert(! dlo_manifest);
1266 assert(! slo_info);
1267
1268 perfcounter->inc(l_rgw_put);
1269 op_ret = -EINVAL;
1270
1271 if (s->object.empty()) {
1272 ldout(s->cct, 0) << __func__ << " called on empty object" << dendl;
1273 goto done;
1274 }
1275
1276 op_ret = get_params();
1277 if (op_ret < 0)
1278 goto done;
1279
1280 op_ret = get_system_versioning_params(s, &olh_epoch, &version_id);
1281 if (op_ret < 0) {
1282 goto done;
1283 }
1284
1285 /* user-supplied MD5 check skipped (not supplied) */
1286 /* early quota check skipped--we don't have size yet */
1287 /* skipping user-supplied etag--we might have one in future, but
1288 * like data it and other attrs would arrive after open */
1289 processor = select_processor(*static_cast<RGWObjectCtx *>(s->obj_ctx),
1290 &multipart);
1291 op_ret = processor->prepare(get_store(), NULL);
1292 if (op_ret < 0) {
1293 ldout(s->cct, 20) << "processor->prepare() returned ret=" << op_ret
1294 << dendl;
1295 goto done;
1296 }
1297
1298 filter = processor;
1299 if (compression_type != "none") {
1300 plugin = Compressor::create(s->cct, compression_type);
1301 if (! plugin) {
1302 ldout(s->cct, 1) << "Cannot load plugin for rgw_compression_type "
1303 << compression_type << dendl;
1304 } else {
1305 compressor.emplace(s->cct, plugin, filter);
1306 filter = &*compressor;
1307 }
1308 }
1309
1310 done:
1311 return op_ret;
1312 } /* exec_start */
1313
1314 int RGWWriteRequest::exec_continue()
1315 {
1316 struct req_state* s = get_state();
1317 op_ret = 0;
1318
1319 /* check guards (e.g., contig write) */
1320 if (eio)
1321 return -EIO;
1322
1323 size_t len = data.length();
1324 if (! len)
1325 return 0;
1326
1327 /* XXX we are currently synchronous--supplied data buffers cannot
1328 * be used after the caller returns */
1329 bool need_to_wait = true;
1330 bufferlist orig_data;
1331
1332 if (need_to_wait) {
1333 orig_data = data;
1334 }
1335 hash.Update((const byte *)data.c_str(), data.length());
1336 op_ret = put_data_and_throttle(filter, data, ofs, need_to_wait);
1337 if (op_ret < 0) {
1338 if (!need_to_wait || op_ret != -EEXIST) {
1339 ldout(s->cct, 20) << "processor->thottle_data() returned ret="
1340 << op_ret << dendl;
1341 goto done;
1342 }
1343
1344 ldout(s->cct, 5) << "NOTICE: processor->throttle_data() returned -EEXIST, need to restart write" << dendl;
1345
1346 /* restore original data */
1347 data.swap(orig_data);
1348
1349 /* restart processing with different oid suffix */
1350 dispose_processor(processor);
1351 processor = select_processor(*static_cast<RGWObjectCtx *>(s->obj_ctx),
1352 &multipart);
1353 filter = processor;
1354
1355 string oid_rand;
1356 char buf[33];
1357 gen_rand_alphanumeric(get_store()->ctx(), buf, sizeof(buf) - 1);
1358 oid_rand.append(buf);
1359
1360 op_ret = processor->prepare(get_store(), &oid_rand);
1361 if (op_ret < 0) {
1362 ldout(s->cct, 0) << "ERROR: processor->prepare() returned "
1363 << op_ret << dendl;
1364 goto done;
1365 }
1366
1367 /* restore compression filter, if any */
1368 if (compressor) {
1369 compressor.emplace(s->cct, plugin, filter);
1370 filter = &*compressor;
1371 }
1372
1373 op_ret = put_data_and_throttle(filter, data, ofs, false);
1374 if (op_ret < 0) {
1375 goto done;
1376 }
1377 }
1378 bytes_written += len;
1379
1380 done:
1381 return op_ret;
1382 } /* exec_continue */
1383
1384 int RGWWriteRequest::exec_finish()
1385 {
1386 buffer::list bl, aclbl, ux_key, ux_attrs;
1387 map<string, string>::iterator iter;
1388 char calc_md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1];
1389 unsigned char m[CEPH_CRYPTO_MD5_DIGESTSIZE];
1390 struct req_state* s = get_state();
1391
1392 size_t osize = rgw_fh->get_size();
1393 struct timespec octime = rgw_fh->get_ctime();
1394 struct timespec omtime = rgw_fh->get_mtime();
1395 real_time appx_t = real_clock::now();
1396
1397 s->obj_size = ofs; // XXX check ofs
1398 perfcounter->inc(l_rgw_put_b, s->obj_size);
1399
1400 op_ret = get_store()->check_quota(s->bucket_owner.get_id(), s->bucket,
1401 user_quota, bucket_quota, s->obj_size);
1402 if (op_ret < 0) {
1403 goto done;
1404 }
1405
1406 op_ret = get_store()->check_bucket_shards(s->bucket_info, s->bucket, bucket_quota);
1407 if (op_ret < 0) {
1408 goto done;
1409 }
1410
1411 hash.Final(m);
1412
1413 if (compressor && compressor->is_compressed()) {
1414 bufferlist tmp;
1415 RGWCompressionInfo cs_info;
1416 cs_info.compression_type = plugin->get_type_name();
1417 cs_info.orig_size = s->obj_size;
1418 cs_info.blocks = std::move(compressor->get_compression_blocks());
1419 ::encode(cs_info, tmp);
1420 attrs[RGW_ATTR_COMPRESSION] = tmp;
1421 ldout(s->cct, 20) << "storing " << RGW_ATTR_COMPRESSION
1422 << " with type=" << cs_info.compression_type
1423 << ", orig_size=" << cs_info.orig_size
1424 << ", blocks=" << cs_info.blocks.size() << dendl;
1425 }
1426
1427 buf_to_hex(m, CEPH_CRYPTO_MD5_DIGESTSIZE, calc_md5);
1428 etag = calc_md5;
1429
1430 bl.append(etag.c_str(), etag.size() + 1);
1431 emplace_attr(RGW_ATTR_ETAG, std::move(bl));
1432
1433 policy.encode(aclbl);
1434 emplace_attr(RGW_ATTR_ACL, std::move(aclbl));
1435
1436 /* unix attrs */
1437 rgw_fh->set_mtime(real_clock::to_timespec(appx_t));
1438 rgw_fh->set_ctime(real_clock::to_timespec(appx_t));
1439 rgw_fh->set_size(bytes_written);
1440 rgw_fh->encode_attrs(ux_key, ux_attrs);
1441
1442 emplace_attr(RGW_ATTR_UNIX_KEY1, std::move(ux_key));
1443 emplace_attr(RGW_ATTR_UNIX1, std::move(ux_attrs));
1444
1445 for (iter = s->generic_attrs.begin(); iter != s->generic_attrs.end();
1446 ++iter) {
1447 buffer::list& attrbl = attrs[iter->first];
1448 const string& val = iter->second;
1449 attrbl.append(val.c_str(), val.size() + 1);
1450 }
1451
1452 rgw_get_request_metadata(s->cct, s->info, attrs);
1453 encode_delete_at_attr(delete_at, attrs);
1454
1455 /* Add a custom metadata to expose the information whether an object
1456 * is an SLO or not. Appending the attribute must be performed AFTER
1457 * processing any input from user in order to prohibit overwriting. */
1458 if (unlikely(!! slo_info)) {
1459 buffer::list slo_userindicator_bl;
1460 ::encode("True", slo_userindicator_bl);
1461 emplace_attr(RGW_ATTR_SLO_UINDICATOR, std::move(slo_userindicator_bl));
1462 }
1463
1464 op_ret = processor->complete(s->obj_size, etag, &mtime, real_time(), attrs,
1465 (delete_at ? *delete_at : real_time()),
1466 if_match, if_nomatch);
1467 if (op_ret != 0) {
1468 /* revert attr updates */
1469 rgw_fh->set_mtime(omtime);
1470 rgw_fh->set_ctime(octime);
1471 rgw_fh->set_size(osize);
1472 }
1473
1474 done:
1475 dispose_processor(processor);
1476 perfcounter->tinc(l_rgw_put_lat,
1477 (ceph_clock_now() - s->time));
1478 return op_ret;
1479 } /* exec_finish */
1480
1481 } /* namespace rgw */
1482
1483 /* librgw */
1484 extern "C" {
1485
1486 void rgwfile_version(int *major, int *minor, int *extra)
1487 {
1488 if (major)
1489 *major = LIBRGW_FILE_VER_MAJOR;
1490 if (minor)
1491 *minor = LIBRGW_FILE_VER_MINOR;
1492 if (extra)
1493 *extra = LIBRGW_FILE_VER_EXTRA;
1494 }
1495
1496 /*
1497 attach rgw namespace
1498 */
1499 int rgw_mount(librgw_t rgw, const char *uid, const char *acc_key,
1500 const char *sec_key, struct rgw_fs **rgw_fs,
1501 uint32_t flags)
1502 {
1503 int rc = 0;
1504
1505 /* stash access data for "mount" */
1506 RGWLibFS* new_fs = new RGWLibFS(static_cast<CephContext*>(rgw), uid, acc_key,
1507 sec_key);
1508 assert(new_fs);
1509
1510 rc = new_fs->authorize(rgwlib.get_store());
1511 if (rc != 0) {
1512 delete new_fs;
1513 return -EINVAL;
1514 }
1515
1516 /* register fs for shared gc */
1517 rgwlib.get_fe()->get_process()->register_fs(new_fs);
1518
1519 struct rgw_fs *fs = new_fs->get_fs();
1520 fs->rgw = rgw;
1521
1522 /* XXX we no longer assume "/" is unique, but we aren't tracking the
1523 * roots atm */
1524
1525 *rgw_fs = fs;
1526
1527 return 0;
1528 }
1529
1530 /*
1531 register invalidate callbacks
1532 */
1533 int rgw_register_invalidate(struct rgw_fs *rgw_fs, rgw_fh_callback_t cb,
1534 void *arg, uint32_t flags)
1535
1536 {
1537 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1538 return fs->register_invalidate(cb, arg, flags);
1539 }
1540
1541 /*
1542 detach rgw namespace
1543 */
1544 int rgw_umount(struct rgw_fs *rgw_fs, uint32_t flags)
1545 {
1546 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1547 fs->close();
1548 return 0;
1549 }
1550
1551 /*
1552 get filesystem attributes
1553 */
1554 int rgw_statfs(struct rgw_fs *rgw_fs,
1555 struct rgw_file_handle *parent_fh,
1556 struct rgw_statvfs *vfs_st, uint32_t flags)
1557 {
1558 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1559
1560 /* XXX for now, just publish a huge capacity and
1561 * limited utiliztion */
1562 vfs_st->f_bsize = 1024*1024 /* 1M */;
1563 vfs_st->f_frsize = 1024; /* minimal allocation unit (who cares) */
1564 vfs_st->f_blocks = UINT64_MAX;
1565 vfs_st->f_bfree = UINT64_MAX;
1566 vfs_st->f_bavail = UINT64_MAX;
1567 vfs_st->f_files = 1024; /* object count, do we have an est? */
1568 vfs_st->f_ffree = UINT64_MAX;
1569 vfs_st->f_fsid[0] = fs->get_inst();
1570 vfs_st->f_fsid[1] = fs->get_inst();
1571 vfs_st->f_flag = 0;
1572 vfs_st->f_namemax = 4096;
1573 return 0;
1574 }
1575
1576 /*
1577 generic create -- create an empty regular file
1578 */
1579 int rgw_create(struct rgw_fs *rgw_fs, struct rgw_file_handle *parent_fh,
1580 const char *name, struct stat *st, uint32_t mask,
1581 struct rgw_file_handle **fh, uint32_t posix_flags,
1582 uint32_t flags)
1583 {
1584 using std::get;
1585
1586 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1587 RGWFileHandle* parent = get_rgwfh(parent_fh);
1588
1589 if ((! parent) ||
1590 (parent->is_root()) ||
1591 (parent->is_file())) {
1592 /* bad parent */
1593 return -EINVAL;
1594 }
1595
1596 MkObjResult fhr = fs->create(parent, name, st, mask, flags);
1597 RGWFileHandle *nfh = get<0>(fhr); // nullptr if !success
1598
1599 if (nfh)
1600 *fh = nfh->get_fh();
1601
1602 return get<1>(fhr);
1603 } /* rgw_create */
1604
1605 /*
1606 create a new directory
1607 */
1608 int rgw_mkdir(struct rgw_fs *rgw_fs,
1609 struct rgw_file_handle *parent_fh,
1610 const char *name, struct stat *st, uint32_t mask,
1611 struct rgw_file_handle **fh, uint32_t flags)
1612 {
1613 using std::get;
1614
1615 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1616 RGWFileHandle* parent = get_rgwfh(parent_fh);
1617
1618 if (! parent) {
1619 /* bad parent */
1620 return -EINVAL;
1621 }
1622
1623 MkObjResult fhr = fs->mkdir(parent, name, st, mask, flags);
1624 RGWFileHandle *nfh = get<0>(fhr); // nullptr if !success
1625
1626 if (nfh)
1627 *fh = nfh->get_fh();
1628
1629 return get<1>(fhr);
1630 } /* rgw_mkdir */
1631
1632 /*
1633 rename object
1634 */
1635 int rgw_rename(struct rgw_fs *rgw_fs,
1636 struct rgw_file_handle *src, const char* src_name,
1637 struct rgw_file_handle *dst, const char* dst_name,
1638 uint32_t flags)
1639 {
1640 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1641
1642 RGWFileHandle* src_fh = get_rgwfh(src);
1643 RGWFileHandle* dst_fh = get_rgwfh(dst);
1644
1645 return fs->rename(src_fh, dst_fh, src_name, dst_name);
1646 }
1647
1648 /*
1649 remove file or directory
1650 */
1651 int rgw_unlink(struct rgw_fs *rgw_fs, struct rgw_file_handle *parent_fh,
1652 const char *name, uint32_t flags)
1653 {
1654 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1655 RGWFileHandle* parent = get_rgwfh(parent_fh);
1656
1657 return fs->unlink(parent, name);
1658 }
1659
1660 /*
1661 lookup object by name (POSIX style)
1662 */
1663 int rgw_lookup(struct rgw_fs *rgw_fs,
1664 struct rgw_file_handle *parent_fh, const char* path,
1665 struct rgw_file_handle **fh, uint32_t flags)
1666 {
1667 //CephContext* cct = static_cast<CephContext*>(rgw_fs->rgw);
1668 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1669
1670 RGWFileHandle* parent = get_rgwfh(parent_fh);
1671 if ((! parent) ||
1672 (! parent->is_dir())) {
1673 /* bad parent */
1674 return -EINVAL;
1675 }
1676
1677 RGWFileHandle* rgw_fh;
1678 LookupFHResult fhr;
1679
1680 if (parent->is_root()) {
1681 /* special: parent lookup--note lack of ref()! */
1682 if (unlikely((strcmp(path, "..") == 0) ||
1683 (strcmp(path, "/") == 0))) {
1684 rgw_fh = parent;
1685 } else {
1686 RGWLibFS::BucketStats bstat;
1687 fhr = fs->stat_bucket(parent, path, bstat, RGWFileHandle::FLAG_NONE);
1688 rgw_fh = get<0>(fhr);
1689 if (! rgw_fh)
1690 return -ENOENT;
1691 }
1692 } else {
1693 /* special: after readdir--note extra ref()! */
1694 if (unlikely((strcmp(path, "..") == 0))) {
1695 rgw_fh = parent;
1696 lsubdout(fs->get_context(), rgw, 17)
1697 << __func__ << "BANG"<< *rgw_fh
1698 << dendl;
1699 fs->ref(rgw_fh);
1700 } else {
1701 /* lookup in a readdir callback */
1702 enum rgw_fh_type fh_type = fh_type_of(flags);
1703
1704 uint32_t sl_flags = (flags & RGW_LOOKUP_FLAG_RCB)
1705 ? RGWFileHandle::FLAG_NONE
1706 : RGWFileHandle::FLAG_EXACT_MATCH;
1707
1708 fhr = fs->stat_leaf(parent, path, fh_type, sl_flags);
1709 if (! get<0>(fhr)) {
1710 if (! (flags & RGW_LOOKUP_FLAG_CREATE))
1711 return -ENOENT;
1712 else
1713 fhr = fs->lookup_fh(parent, path, RGWFileHandle::FLAG_CREATE);
1714 }
1715 rgw_fh = get<0>(fhr);
1716 }
1717 } /* !root */
1718
1719 struct rgw_file_handle *rfh = rgw_fh->get_fh();
1720 *fh = rfh;
1721
1722 return 0;
1723 } /* rgw_lookup */
1724
1725 /*
1726 lookup object by handle (NFS style)
1727 */
1728 int rgw_lookup_handle(struct rgw_fs *rgw_fs, struct rgw_fh_hk *fh_hk,
1729 struct rgw_file_handle **fh, uint32_t flags)
1730 {
1731 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1732
1733 RGWFileHandle* rgw_fh = fs->lookup_handle(*fh_hk);
1734 if (! rgw_fh) {
1735 /* not found */
1736 return -ENOENT;
1737 }
1738
1739 struct rgw_file_handle *rfh = rgw_fh->get_fh();
1740 *fh = rfh;
1741
1742 return 0;
1743 }
1744
1745 /*
1746 * release file handle
1747 */
1748 int rgw_fh_rele(struct rgw_fs *rgw_fs, struct rgw_file_handle *fh,
1749 uint32_t flags)
1750 {
1751 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1752 RGWFileHandle* rgw_fh = get_rgwfh(fh);
1753
1754 lsubdout(fs->get_context(), rgw, 17)
1755 << __func__ << " " << *rgw_fh
1756 << dendl;
1757
1758 fs->unref(rgw_fh);
1759 return 0;
1760 }
1761
1762 /*
1763 get unix attributes for object
1764 */
1765 int rgw_getattr(struct rgw_fs *rgw_fs,
1766 struct rgw_file_handle *fh, struct stat *st, uint32_t flags)
1767 {
1768 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1769 RGWFileHandle* rgw_fh = get_rgwfh(fh);
1770
1771 return fs->getattr(rgw_fh, st);
1772 }
1773
1774 /*
1775 set unix attributes for object
1776 */
1777 int rgw_setattr(struct rgw_fs *rgw_fs,
1778 struct rgw_file_handle *fh, struct stat *st,
1779 uint32_t mask, uint32_t flags)
1780 {
1781 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1782 RGWFileHandle* rgw_fh = get_rgwfh(fh);
1783
1784 return fs->setattr(rgw_fh, st, mask, flags);
1785 }
1786
1787 /*
1788 truncate file
1789 */
1790 int rgw_truncate(struct rgw_fs *rgw_fs,
1791 struct rgw_file_handle *fh, uint64_t size, uint32_t flags)
1792 {
1793 return 0;
1794 }
1795
1796 /*
1797 open file
1798 */
1799 int rgw_open(struct rgw_fs *rgw_fs,
1800 struct rgw_file_handle *fh, uint32_t posix_flags, uint32_t flags)
1801 {
1802 RGWFileHandle* rgw_fh = get_rgwfh(fh);
1803
1804 /* XXX
1805 * need to track specific opens--at least read opens and
1806 * a write open; we need to know when a write open is returned,
1807 * that closes a write transaction
1808 *
1809 * for now, we will support single-open only, it's preferable to
1810 * anything we can otherwise do without access to the NFS state
1811 */
1812 if (! rgw_fh->is_file())
1813 return -EISDIR;
1814
1815 return rgw_fh->open(flags);
1816 }
1817
1818 /*
1819 close file
1820 */
1821 int rgw_close(struct rgw_fs *rgw_fs,
1822 struct rgw_file_handle *fh, uint32_t flags)
1823 {
1824 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1825 RGWFileHandle* rgw_fh = get_rgwfh(fh);
1826 int rc = rgw_fh->close(/* XXX */);
1827
1828 if (flags & RGW_CLOSE_FLAG_RELE)
1829 fs->unref(rgw_fh);
1830
1831 return rc;
1832 }
1833
1834 int rgw_readdir(struct rgw_fs *rgw_fs,
1835 struct rgw_file_handle *parent_fh, uint64_t *offset,
1836 rgw_readdir_cb rcb, void *cb_arg, bool *eof,
1837 uint32_t flags)
1838 {
1839 RGWFileHandle* parent = get_rgwfh(parent_fh);
1840 if (! parent) {
1841 /* bad parent */
1842 return -EINVAL;
1843 }
1844 int rc = parent->readdir(rcb, cb_arg, offset, eof, flags);
1845 return rc;
1846 }
1847
1848 /* project offset of dirent name */
1849 int rgw_dirent_offset(struct rgw_fs *rgw_fs,
1850 struct rgw_file_handle *parent_fh,
1851 const char *name, int64_t *offset,
1852 uint32_t flags)
1853 {
1854 RGWFileHandle* parent = get_rgwfh(parent_fh);
1855 if ((! parent)) {
1856 /* bad parent */
1857 return -EINVAL;
1858 }
1859 std::string sname{name};
1860 int rc = parent->offset_of(sname, offset, flags);
1861 return rc;
1862 }
1863
1864 /*
1865 read data from file
1866 */
1867 int rgw_read(struct rgw_fs *rgw_fs,
1868 struct rgw_file_handle *fh, uint64_t offset,
1869 size_t length, size_t *bytes_read, void *buffer,
1870 uint32_t flags)
1871 {
1872 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1873 RGWFileHandle* rgw_fh = get_rgwfh(fh);
1874
1875 return fs->read(rgw_fh, offset, length, bytes_read, buffer, flags);
1876 }
1877
1878 /*
1879 write data to file
1880 */
1881 int rgw_write(struct rgw_fs *rgw_fs,
1882 struct rgw_file_handle *fh, uint64_t offset,
1883 size_t length, size_t *bytes_written, void *buffer,
1884 uint32_t flags)
1885 {
1886 RGWFileHandle* rgw_fh = get_rgwfh(fh);
1887 int rc;
1888
1889 *bytes_written = 0;
1890
1891 if (! rgw_fh->is_file())
1892 return -EISDIR;
1893
1894 if (! rgw_fh->is_open())
1895 return -EPERM;
1896
1897 rc = rgw_fh->write(offset, length, bytes_written, buffer);
1898
1899 return rc;
1900 }
1901
1902 /*
1903 read data from file (vector)
1904 */
1905 class RGWReadV
1906 {
1907 buffer::list bl;
1908 struct rgw_vio* vio;
1909
1910 public:
1911 RGWReadV(buffer::list& _bl, rgw_vio* _vio) : vio(_vio) {
1912 bl.claim(_bl);
1913 }
1914
1915 struct rgw_vio* get_vio() { return vio; }
1916
1917 const std::list<buffer::ptr>& buffers() { return bl.buffers(); }
1918
1919 unsigned /* XXX */ length() { return bl.length(); }
1920
1921 };
1922
1923 void rgw_readv_rele(struct rgw_uio *uio, uint32_t flags)
1924 {
1925 RGWReadV* rdv = static_cast<RGWReadV*>(uio->uio_p1);
1926 rdv->~RGWReadV();
1927 ::operator delete(rdv);
1928 }
1929
1930 int rgw_readv(struct rgw_fs *rgw_fs,
1931 struct rgw_file_handle *fh, rgw_uio *uio, uint32_t flags)
1932 {
1933 #if 0 /* XXX */
1934 CephContext* cct = static_cast<CephContext*>(rgw_fs->rgw);
1935 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1936 RGWFileHandle* rgw_fh = get_rgwfh(fh);
1937
1938 if (! rgw_fh->is_file())
1939 return -EINVAL;
1940
1941 int rc = 0;
1942
1943 buffer::list bl;
1944 RGWGetObjRequest req(cct, fs->get_user(), rgw_fh->bucket_name(),
1945 rgw_fh->object_name(), uio->uio_offset, uio->uio_resid,
1946 bl);
1947 req.do_hexdump = false;
1948
1949 rc = rgwlib.get_fe()->execute_req(&req);
1950
1951 if (! rc) {
1952 RGWReadV* rdv = static_cast<RGWReadV*>(
1953 ::operator new(sizeof(RGWReadV) +
1954 (bl.buffers().size() * sizeof(struct rgw_vio))));
1955
1956 (void) new (rdv)
1957 RGWReadV(bl, reinterpret_cast<rgw_vio*>(rdv+sizeof(RGWReadV)));
1958
1959 uio->uio_p1 = rdv;
1960 uio->uio_cnt = rdv->buffers().size();
1961 uio->uio_resid = rdv->length();
1962 uio->uio_vio = rdv->get_vio();
1963 uio->uio_rele = rgw_readv_rele;
1964
1965 int ix = 0;
1966 auto& buffers = rdv->buffers();
1967 for (auto& bp : buffers) {
1968 rgw_vio *vio = &(uio->uio_vio[ix]);
1969 vio->vio_base = const_cast<char*>(bp.c_str());
1970 vio->vio_len = bp.length();
1971 vio->vio_u1 = nullptr;
1972 vio->vio_p1 = nullptr;
1973 ++ix;
1974 }
1975 }
1976
1977 return rc;
1978 #else
1979 return 0;
1980 #endif
1981 }
1982
1983 /*
1984 write data to file (vector)
1985 */
1986 int rgw_writev(struct rgw_fs *rgw_fs, struct rgw_file_handle *fh,
1987 rgw_uio *uio, uint32_t flags)
1988 {
1989
1990 return -ENOTSUP;
1991
1992 CephContext* cct = static_cast<CephContext*>(rgw_fs->rgw);
1993 RGWLibFS *fs = static_cast<RGWLibFS*>(rgw_fs->fs_private);
1994 RGWFileHandle* rgw_fh = get_rgwfh(fh);
1995
1996 if (! rgw_fh->is_file())
1997 return -EINVAL;
1998
1999 buffer::list bl;
2000 for (unsigned int ix = 0; ix < uio->uio_cnt; ++ix) {
2001 rgw_vio *vio = &(uio->uio_vio[ix]);
2002 bl.push_back(
2003 buffer::create_static(vio->vio_len,
2004 static_cast<char*>(vio->vio_base)));
2005 }
2006
2007 std::string oname = rgw_fh->relative_object_name();
2008 RGWPutObjRequest req(cct, fs->get_user(), rgw_fh->bucket_name(),
2009 oname, bl);
2010
2011 int rc = rgwlib.get_fe()->execute_req(&req);
2012
2013 /* XXX update size (in request) */
2014
2015 return rc;
2016 }
2017
2018 /*
2019 sync written data
2020 */
2021 int rgw_fsync(struct rgw_fs *rgw_fs, struct rgw_file_handle *handle,
2022 uint32_t flags)
2023 {
2024 return 0;
2025 }
2026
2027 int rgw_commit(struct rgw_fs *rgw_fs, struct rgw_file_handle *fh,
2028 uint64_t offset, uint64_t length, uint32_t flags)
2029 {
2030 RGWFileHandle* rgw_fh = get_rgwfh(fh);
2031
2032 return rgw_fh->commit(offset, length, RGWFileHandle::FLAG_NONE);
2033 }
2034
2035 } /* extern "C" */