1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
22 #if defined(__FreeBSD__)
23 #include <sys/param.h>
26 #include "osd/osd_types.h"
27 #include "include/object.h"
28 #include "common/config.h"
29 #include "common/debug.h"
30 #include "include/buffer.h"
31 #include "common/ceph_crypto.h"
32 #include "include/compat.h"
33 #include "chain_xattr.h"
36 using ceph::crypto::SHA1;
38 #define dout_context cct
39 #define dout_subsys ceph_subsys_filestore
41 #define dout_prefix *_dout << "LFNIndex(" << get_base_path() << ") "
44 const string LFNIndex::LFN_ATTR = "user.cephos.lfn";
45 const string LFNIndex::PHASH_ATTR_PREFIX = "user.cephos.phash.";
46 const string LFNIndex::SUBDIR_PREFIX = "DIR_";
47 const string LFNIndex::FILENAME_COOKIE = "long";
48 const int LFNIndex::FILENAME_PREFIX_LEN = FILENAME_SHORT_LEN - FILENAME_HASH_LEN -
49 FILENAME_COOKIE.size() -
51 void LFNIndex::maybe_inject_failure()
53 if (error_injection_enabled) {
54 if (current_failure > last_failure &&
55 (((double)(rand() % 10000))/((double)(10000))
56 < error_injection_probability)) {
57 last_failure = current_failure;
59 throw RetryException();
65 // Helper to close fd's when we leave scope. This is useful when used
66 // in combination with RetryException, thrown by the above.
69 explicit FDCloser(int f) : fd(f) {}
71 VOID_TEMP_FAILURE_RETRY(::close(fd));
78 uint64_t LFNIndex::get_max_escaped_name_len(const hobject_t &obj)
80 ghobject_t ghobj(obj);
81 ghobj.shard_id = shard_id_t(0);
84 return lfn_generate_object_name_current(ghobj).size();
92 int LFNIndex::created(const ghobject_t &oid, const char *path)
95 vector<string> path_comp;
97 r = decompose_full_path(path, &path_comp, 0, &short_name);
100 r = lfn_created(path_comp, oid, short_name);
103 /* This is hacky, but the only way we get ENOENT from lfn_created here is
104 * if we did a failure injection in _created below AND actually started the
105 * split or merge. In that case, lfn_created already suceeded, and
106 * WRAP_RETRY already cleaned it up and we are actually done. In a real
107 * failure, the filestore itself would have ended up calling this with
108 * the new path, not the old one, so we'd find it.
114 r = _created(path_comp, oid, short_name);
120 int LFNIndex::unlink(const ghobject_t &oid)
125 r = _lookup(oid, &path, &short_name, NULL);
129 r = _remove(path, oid, short_name);
136 int LFNIndex::lookup(const ghobject_t &oid,
137 IndexedPath *out_path,
143 r = _lookup(oid, &path, &short_name, hardlink);
146 string full_path = get_full_path(path, short_name);
147 *out_path = std::make_shared<Path>(full_path, this);
152 int LFNIndex::pre_hash_collection(uint32_t pg_num, uint64_t expected_num_objs)
154 return _pre_hash_collection(pg_num, expected_num_objs);
158 int LFNIndex::collection_list_partial(const ghobject_t &start,
159 const ghobject_t &end,
161 vector<ghobject_t> *ls,
164 return _collection_list_partial(start, end, max_count, ls, next);
167 /* Derived class utility methods */
169 int LFNIndex::fsync_dir(const vector<string> &path)
171 maybe_inject_failure();
172 int fd = ::open(get_full_path_subdir(path).c_str(), O_RDONLY);
176 maybe_inject_failure();
178 maybe_inject_failure();
185 int LFNIndex::link_object(const vector<string> &from,
186 const vector<string> &to,
187 const ghobject_t &oid,
188 const string &from_short_name)
191 string from_path = get_full_path(from, from_short_name);
193 maybe_inject_failure();
194 r = lfn_get_name(to, oid, 0, &to_path, 0);
197 maybe_inject_failure();
198 r = ::link(from_path.c_str(), to_path.c_str());
199 maybe_inject_failure();
206 int LFNIndex::remove_objects(const vector<string> &dir,
207 const map<string, ghobject_t> &to_remove,
208 map<string, ghobject_t> *remaining)
210 set<string> clean_chains;
211 for (map<string, ghobject_t>::const_iterator to_clean = to_remove.begin();
212 to_clean != to_remove.end();
214 if (!lfn_is_hashed_filename(to_clean->first)) {
215 maybe_inject_failure();
216 int r = ::unlink(get_full_path(dir, to_clean->first).c_str());
217 maybe_inject_failure();
222 if (clean_chains.count(lfn_get_short_name(to_clean->second, 0)))
225 map<int, pair<string, ghobject_t> > chain;
226 for (int i = 0; ; ++i) {
227 string short_name = lfn_get_short_name(to_clean->second, i);
228 if (remaining->count(short_name)) {
229 chain[i] = *(remaining->find(short_name));
230 } else if (to_remove.count(short_name)) {
237 map<int, pair<string, ghobject_t > >::reverse_iterator candidate = chain.rbegin();
238 for (set<int>::iterator i = holes.begin();
241 if (candidate == chain.rend() || *i > candidate->first) {
242 string remove_path_name =
243 get_full_path(dir, lfn_get_short_name(to_clean->second, *i));
244 maybe_inject_failure();
245 int r = ::unlink(remove_path_name.c_str());
246 maybe_inject_failure();
251 string from = get_full_path(dir, candidate->second.first);
252 string to = get_full_path(dir, lfn_get_short_name(candidate->second.second, *i));
253 maybe_inject_failure();
254 int r = ::rename(from.c_str(), to.c_str());
255 maybe_inject_failure();
258 remaining->erase(candidate->second.first);
259 remaining->insert(pair<string, ghobject_t>(
260 lfn_get_short_name(candidate->second.second, *i),
261 candidate->second.second));
265 clean_chains.insert(lfn_get_short_name(to_clean->second, 0));
270 int LFNIndex::move_objects(const vector<string> &from,
271 const vector<string> &to)
273 map<string, ghobject_t> to_move;
275 r = list_objects(from, 0, NULL, &to_move);
278 for (map<string,ghobject_t>::iterator i = to_move.begin();
281 string from_path = get_full_path(from, i->first);
282 string to_path, to_name;
283 r = lfn_get_name(to, i->second, &to_name, &to_path, 0);
286 maybe_inject_failure();
287 r = ::link(from_path.c_str(), to_path.c_str());
288 if (r < 0 && errno != EEXIST)
290 maybe_inject_failure();
291 r = lfn_created(to, i->second, to_name);
292 maybe_inject_failure();
299 for (map<string,ghobject_t>::iterator i = to_move.begin();
302 maybe_inject_failure();
303 r = ::unlink(get_full_path(from, i->first).c_str());
304 maybe_inject_failure();
308 return fsync_dir(from);
311 int LFNIndex::remove_object(const vector<string> &from,
312 const ghobject_t &oid)
316 maybe_inject_failure();
317 r = get_mangled_name(from, oid, &short_name, &exist);
318 maybe_inject_failure();
323 return lfn_unlink(from, oid, short_name);
326 int LFNIndex::get_mangled_name(const vector<string> &from,
327 const ghobject_t &oid,
328 string *mangled_name, int *hardlink)
330 return lfn_get_name(from, oid, mangled_name, 0, hardlink);
333 int LFNIndex::move_subdir(
336 const vector<string> &path,
340 vector<string> sub_path(path.begin(), path.end());
341 sub_path.push_back(dir);
342 string from_path(from.get_full_path_subdir(sub_path));
343 string to_path(dest.get_full_path_subdir(sub_path));
344 int r = ::rename(from_path.c_str(), to_path.c_str());
350 int LFNIndex::move_object(
353 const vector<string> &path,
354 const pair<string, ghobject_t> &obj
357 string from_path(from.get_full_path(path, obj.first));
361 int r = dest.lfn_get_name(path, obj.second, &to_name, &to_path, &exists);
365 r = ::link(from_path.c_str(), to_path.c_str());
369 r = dest.lfn_created(path, obj.second, to_name);
372 r = dest.fsync_dir(path);
375 r = from.remove_object(path, obj.second);
378 return from.fsync_dir(path);
382 static int get_hobject_from_oinfo(const char *dir, const char *file,
386 snprintf(path, sizeof(path), "%s/%s", dir, file);
387 // Hack, user.ceph._ is the attribute used to store the object info
389 int r = chain_getxattr_buf(
398 object_info_t oi(bl);
399 *o = ghobject_t(oi.soid);
404 int LFNIndex::list_objects(const vector<string> &to_list, int max_objs,
405 long *handle, map<string, ghobject_t> *out)
407 string to_list_path = get_full_path_subdir(to_list);
408 DIR *dir = ::opendir(to_list_path.c_str());
413 if (handle && *handle) {
414 seekdir(dir, *handle);
417 struct dirent *de = nullptr;
421 while ((de = ::readdir(dir))) {
423 if (max_objs > 0 && listed >= max_objs) {
426 if (de->d_name[0] == '.')
428 string short_name(de->d_name);
430 if (lfn_is_object(short_name)) {
431 r = lfn_translate(to_list, short_name, &obj);
437 string long_name = lfn_generate_object_name(obj);
438 if (!lfn_must_hash(long_name)) {
439 assert(long_name == short_name);
441 if (index_version == HASH_INDEX_TAG)
442 get_hobject_from_oinfo(to_list_path.c_str(), short_name.c_str(), &obj);
444 out->insert(pair<string, ghobject_t>(short_name, obj));
450 if (handle && !end) {
451 *handle = telldir(dir);
460 int LFNIndex::list_subdirs(const vector<string> &to_list,
463 string to_list_path = get_full_path_subdir(to_list);
464 DIR *dir = ::opendir(to_list_path.c_str());
468 struct dirent *de = nullptr;
469 while ((de = ::readdir(dir))) {
470 string short_name(de->d_name);
471 string demangled_name;
472 if (lfn_is_subdir(short_name, &demangled_name)) {
473 out->push_back(demangled_name);
481 int LFNIndex::create_path(const vector<string> &to_create)
483 maybe_inject_failure();
484 int r = ::mkdir(get_full_path_subdir(to_create).c_str(), 0777);
485 maybe_inject_failure();
492 int LFNIndex::remove_path(const vector<string> &to_remove)
494 maybe_inject_failure();
495 int r = ::rmdir(get_full_path_subdir(to_remove).c_str());
496 maybe_inject_failure();
503 int LFNIndex::path_exists(const vector<string> &to_check, int *exists)
505 string full_path = get_full_path_subdir(to_check);
507 if (::stat(full_path.c_str(), &buf)) {
521 int LFNIndex::add_attr_path(const vector<string> &path,
522 const string &attr_name,
523 bufferlist &attr_value)
525 string full_path = get_full_path_subdir(path);
526 maybe_inject_failure();
527 return chain_setxattr<false, true>(
528 full_path.c_str(), mangle_attr_name(attr_name).c_str(),
529 reinterpret_cast<void *>(attr_value.c_str()),
530 attr_value.length());
533 int LFNIndex::get_attr_path(const vector<string> &path,
534 const string &attr_name,
535 bufferlist &attr_value)
537 string full_path = get_full_path_subdir(path);
539 int r = chain_getxattr_buf(
541 mangle_attr_name(attr_name).c_str(),
544 attr_value.push_back(bp);
548 int LFNIndex::remove_attr_path(const vector<string> &path,
549 const string &attr_name)
551 string full_path = get_full_path_subdir(path);
552 string mangled_attr_name = mangle_attr_name(attr_name);
553 maybe_inject_failure();
554 return chain_removexattr(full_path.c_str(), mangled_attr_name.c_str());
557 string LFNIndex::lfn_generate_object_name_keyless(const ghobject_t &oid)
559 char s[FILENAME_MAX_LEN];
560 char *end = s + sizeof(s);
563 assert(oid.generation == ghobject_t::NO_GEN);
564 const char *i = oid.hobj.oid.name.c_str();
565 // Escape subdir prefix
566 if (oid.hobj.oid.name.substr(0, 4) == "DIR_") {
571 while (*i && t < end) {
575 } else if (*i == '.' && i == oid.hobj.oid.name.c_str()) { // only escape leading .
578 } else if (*i == '/') {
586 if (oid.hobj.snap == CEPH_NOSNAP)
587 t += snprintf(t, end - t, "_head");
588 else if (oid.hobj.snap == CEPH_SNAPDIR)
589 t += snprintf(t, end - t, "_snapdir");
591 t += snprintf(t, end - t, "_%llx", (long long unsigned)oid.hobj.snap);
592 snprintf(t, end - t, "_%.*X", (int)(sizeof(oid.hobj.get_hash())*2), oid.hobj.get_hash());
597 static void append_escaped(string::const_iterator begin,
598 string::const_iterator end,
601 for (string::const_iterator i = begin; i != end; ++i) {
604 } else if (*i == '/') {
606 } else if (*i == '_') {
608 } else if (*i == '\0') {
616 string LFNIndex::lfn_generate_object_name_current(const ghobject_t &oid)
619 string::const_iterator i = oid.hobj.oid.name.begin();
620 if (oid.hobj.oid.name.substr(0, 4) == "DIR_") {
621 full_name.append("\\d");
623 } else if (oid.hobj.oid.name[0] == '.') {
624 full_name.append("\\.");
627 append_escaped(i, oid.hobj.oid.name.end(), &full_name);
628 full_name.append("_");
629 append_escaped(oid.hobj.get_key().begin(), oid.hobj.get_key().end(), &full_name);
630 full_name.append("_");
634 const char *end = t + sizeof(buf);
635 if (oid.hobj.snap == CEPH_NOSNAP)
636 t += snprintf(t, end - t, "head");
637 else if (oid.hobj.snap == CEPH_SNAPDIR)
638 t += snprintf(t, end - t, "snapdir");
640 t += snprintf(t, end - t, "%llx", (long long unsigned)oid.hobj.snap);
641 t += snprintf(t, end - t, "_%.*X", (int)(sizeof(oid.hobj.get_hash())*2), oid.hobj.get_hash());
642 full_name.append(buf, t);
643 full_name.append("_");
645 append_escaped(oid.hobj.nspace.begin(), oid.hobj.nspace.end(), &full_name);
646 full_name.append("_");
649 if (oid.hobj.pool == -1)
650 t += snprintf(t, end - t, "none");
652 t += snprintf(t, end - t, "%llx", (long long unsigned)oid.hobj.pool);
653 full_name.append(buf, t);
655 if (oid.generation != ghobject_t::NO_GEN ||
656 oid.shard_id != shard_id_t::NO_SHARD) {
657 full_name.append("_");
660 t += snprintf(t, end - buf, "%llx", (long long unsigned)oid.generation);
661 full_name.append(buf, t);
663 full_name.append("_");
666 t += snprintf(t, end - buf, "%x", (int)oid.shard_id);
667 full_name.append(buf, t);
673 string LFNIndex::lfn_generate_object_name_poolless(const ghobject_t &oid)
675 if (index_version == HASH_INDEX_TAG)
676 return lfn_generate_object_name_keyless(oid);
678 assert(oid.generation == ghobject_t::NO_GEN);
680 string::const_iterator i = oid.hobj.oid.name.begin();
681 if (oid.hobj.oid.name.substr(0, 4) == "DIR_") {
682 full_name.append("\\d");
684 } else if (oid.hobj.oid.name[0] == '.') {
685 full_name.append("\\.");
688 append_escaped(i, oid.hobj.oid.name.end(), &full_name);
689 full_name.append("_");
690 append_escaped(oid.hobj.get_key().begin(), oid.hobj.get_key().end(), &full_name);
691 full_name.append("_");
693 char snap_with_hash[PATH_MAX];
694 char *t = snap_with_hash;
695 char *end = t + sizeof(snap_with_hash);
696 if (oid.hobj.snap == CEPH_NOSNAP)
697 t += snprintf(t, end - t, "head");
698 else if (oid.hobj.snap == CEPH_SNAPDIR)
699 t += snprintf(t, end - t, "snapdir");
701 t += snprintf(t, end - t, "%llx", (long long unsigned)oid.hobj.snap);
702 snprintf(t, end - t, "_%.*X", (int)(sizeof(oid.hobj.get_hash())*2), oid.hobj.get_hash());
703 full_name += string(snap_with_hash);
707 int LFNIndex::lfn_get_name(const vector<string> &path,
708 const ghobject_t &oid,
709 string *mangled_name, string *out_path,
712 string full_name = lfn_generate_object_name(oid);
715 if (!lfn_must_hash(full_name)) {
717 *mangled_name = full_name;
719 *out_path = get_full_path(path, full_name);
722 string full_path = get_full_path(path, full_name);
723 maybe_inject_failure();
724 r = ::stat(full_path.c_str(), &buf);
731 *hardlink = buf.st_nlink;
739 string candidate_path;
741 candidate = lfn_get_short_name(oid, i);
742 candidate_path = get_full_path(path, candidate);
744 r = chain_getxattr_buf(
745 candidate_path.c_str(),
746 get_lfn_attr().c_str(),
749 if (errno != ENODATA && errno != ENOENT)
751 if (errno == ENODATA) {
752 // Left over from incomplete transaction, it'll be replayed
753 maybe_inject_failure();
754 r = ::unlink(candidate_path.c_str());
755 maybe_inject_failure();
760 *mangled_name = candidate;
762 *out_path = candidate_path;
768 string lfn(bp.c_str(), bp.length());
769 if (lfn == full_name) {
771 *mangled_name = candidate;
773 *out_path = candidate_path;
776 r = ::stat(candidate_path.c_str(), &st);
783 *hardlink = st.st_nlink;
789 r = chain_getxattr_buf(
790 candidate_path.c_str(),
791 get_alt_lfn_attr().c_str(),
794 // only consider alt name if nlink > 1
796 int rc = ::stat(candidate_path.c_str(), &st);
799 if (st.st_nlink <= 1) {
800 // left over from incomplete unlink, remove
801 maybe_inject_failure();
802 dout(20) << __func__ << " found extra alt attr for " << candidate_path
803 << ", long name " << string(bp.c_str(), bp.length()) << dendl;
804 rc = chain_removexattr(candidate_path.c_str(),
805 get_alt_lfn_attr().c_str());
806 maybe_inject_failure();
811 string lfn(bp.c_str(), bp.length());
812 if (lfn == full_name) {
813 dout(20) << __func__ << " used alt attr for " << full_name << dendl;
815 *mangled_name = candidate;
817 *out_path = candidate_path;
819 *hardlink = st.st_nlink;
824 ceph_abort(); // Unreachable
828 int LFNIndex::lfn_created(const vector<string> &path,
829 const ghobject_t &oid,
830 const string &mangled_name)
832 if (!lfn_is_hashed_filename(mangled_name))
834 string full_path = get_full_path(path, mangled_name);
835 string full_name = lfn_generate_object_name(oid);
836 maybe_inject_failure();
838 // if the main attr exists and is different, move it to the alt attr.
840 int r = chain_getxattr_buf(
842 get_lfn_attr().c_str(),
845 string lfn(bp.c_str(), bp.length());
846 if (lfn != full_name) {
847 dout(20) << __func__ << " " << mangled_name
848 << " moving old name to alt attr "
850 << ", new name is " << full_name << dendl;
851 r = chain_setxattr<false, true>(
852 full_path.c_str(), get_alt_lfn_attr().c_str(),
853 bp.c_str(), bp.length());
859 return chain_setxattr<false, true>(
860 full_path.c_str(), get_lfn_attr().c_str(),
861 full_name.c_str(), full_name.size());
864 int LFNIndex::lfn_unlink(const vector<string> &path,
865 const ghobject_t &oid,
866 const string &mangled_name)
868 if (!lfn_is_hashed_filename(mangled_name)) {
869 string full_path = get_full_path(path, mangled_name);
870 maybe_inject_failure();
871 int r = ::unlink(full_path.c_str());
872 maybe_inject_failure();
880 string candidate = lfn_get_short_name(oid, i);
881 if (candidate == mangled_name)
884 int removed_index = i;
888 string to_check = lfn_get_short_name(oid, i);
889 string to_check_path = get_full_path(path, to_check);
890 int r = ::stat(to_check_path.c_str(), &buf);
892 if (errno == ENOENT) {
899 string full_path = get_full_path(path, mangled_name);
900 int fd = ::open(full_path.c_str(), O_RDONLY);
904 if (i == removed_index + 1) {
905 maybe_inject_failure();
906 int r = ::unlink(full_path.c_str());
907 maybe_inject_failure();
911 string& rename_to = full_path;
912 string rename_from = get_full_path(path, lfn_get_short_name(oid, i - 1));
913 maybe_inject_failure();
914 int r = ::rename(rename_from.c_str(), rename_to.c_str());
915 maybe_inject_failure();
920 int r = ::fstat(fd, &st);
921 if (r == 0 && st.st_nlink > 0) {
923 dout(20) << __func__ << " removing alt attr from " << full_path << dendl;
925 chain_fremovexattr(fd, get_alt_lfn_attr().c_str());
930 int LFNIndex::lfn_translate(const vector<string> &path,
931 const string &short_name,
934 if (!lfn_is_hashed_filename(short_name)) {
935 return lfn_parse_object_name(short_name, out);
937 string full_path = get_full_path(path, short_name);
938 // First, check alt attr
940 int r = chain_getxattr_buf(
942 get_alt_lfn_attr().c_str(),
945 // There is an alt attr, does it match?
946 string lfn(bp.c_str(), bp.length());
947 if (short_name_matches(short_name.c_str(), lfn.c_str())) {
948 return lfn_parse_object_name(lfn, out);
954 r = chain_getxattr_buf(
956 get_lfn_attr().c_str(),
963 string long_name(bp.c_str(), bp.length());
964 return lfn_parse_object_name(long_name, out);
967 bool LFNIndex::lfn_is_object(const string &short_name)
969 return lfn_is_hashed_filename(short_name) || !lfn_is_subdir(short_name, 0);
972 bool LFNIndex::lfn_is_subdir(const string &name, string *demangled)
974 if (name.substr(0, SUBDIR_PREFIX.size()) == SUBDIR_PREFIX) {
976 *demangled = demangle_path_component(name);
982 static int parse_object(const char *s, ghobject_t& o)
984 const char *hash = s + strlen(s) - 1;
985 while (*hash != '_' &&
988 const char *bar = hash - 1;
989 while (*bar != '_' &&
1000 case '\\': *t++ = '\\'; break;
1001 case '.': *t++ = '.'; break;
1002 case 's': *t++ = '/'; break;
1010 default: ceph_abort();
1018 o.hobj.oid.name = string(buf, t-buf);
1019 if (strncmp(bar+1, "head", 4) == 0)
1020 o.hobj.snap = CEPH_NOSNAP;
1021 else if (strncmp(bar+1, "snapdir", 7) == 0)
1022 o.hobj.snap = CEPH_SNAPDIR;
1024 o.hobj.snap = strtoull(bar+1, NULL, 16);
1026 uint32_t hobject_hash_input;
1027 sscanf(hash, "_%X", &hobject_hash_input);
1028 o.hobj.set_hash(hobject_hash_input);
1035 int LFNIndex::lfn_parse_object_name_keyless(const string &long_name, ghobject_t *out)
1037 int r = parse_object(long_name.c_str(), *out);
1040 if (coll().is_pg_prefix(&pg))
1041 pool = (int64_t)pg.pgid.pool();
1042 out->hobj.pool = pool;
1043 if (!r) return -EINVAL;
1044 string temp = lfn_generate_object_name(*out);
1045 return r ? 0 : -EINVAL;
1048 static bool append_unescaped(string::const_iterator begin,
1049 string::const_iterator end,
1052 for (string::const_iterator i = begin; i != end; ++i) {
1066 out->append(i, i+1);
1072 int LFNIndex::lfn_parse_object_name_poolless(const string &long_name,
1080 string::const_iterator current = long_name.begin();
1081 if (*current == '\\') {
1083 if (current == long_name.end()) {
1085 } else if (*current == 'd') {
1086 name.append("DIR_");
1088 } else if (*current == '.') {
1096 string::const_iterator end = current;
1097 for ( ; end != long_name.end() && *end != '_'; ++end) ;
1098 if (end == long_name.end())
1100 if (!append_unescaped(current, end, &name))
1104 for ( ; end != long_name.end() && *end != '_'; ++end) ;
1105 if (end == long_name.end())
1107 if (!append_unescaped(current, end, &key))
1111 for ( ; end != long_name.end() && *end != '_'; ++end) ;
1112 if (end == long_name.end())
1114 string snap_str(current, end);
1117 for ( ; end != long_name.end() && *end != '_'; ++end) ;
1118 if (end != long_name.end())
1120 string hash_str(current, end);
1122 if (snap_str == "head")
1124 else if (snap_str == "snapdir")
1125 snap = CEPH_SNAPDIR;
1127 snap = strtoull(snap_str.c_str(), NULL, 16);
1128 sscanf(hash_str.c_str(), "%X", &hash);
1133 if (coll().is_pg_prefix(&pg))
1134 pool = (int64_t)pg.pgid.pool();
1135 (*out) = ghobject_t(hobject_t(name, key, snap, hash, pool, ""));
1140 int LFNIndex::lfn_parse_object_name(const string &long_name, ghobject_t *out)
1148 gen_t generation = ghobject_t::NO_GEN;
1149 shard_id_t shard_id = shard_id_t::NO_SHARD;
1151 if (index_version == HASH_INDEX_TAG)
1152 return lfn_parse_object_name_keyless(long_name, out);
1153 if (index_version == HASH_INDEX_TAG_2)
1154 return lfn_parse_object_name_poolless(long_name, out);
1156 string::const_iterator current = long_name.begin();
1157 if (*current == '\\') {
1159 if (current == long_name.end()) {
1161 } else if (*current == 'd') {
1162 name.append("DIR_");
1164 } else if (*current == '.') {
1172 string::const_iterator end = current;
1173 for ( ; end != long_name.end() && *end != '_'; ++end) ;
1174 if (end == long_name.end())
1176 if (!append_unescaped(current, end, &name))
1180 for ( ; end != long_name.end() && *end != '_'; ++end) ;
1181 if (end == long_name.end())
1183 if (!append_unescaped(current, end, &key))
1187 for ( ; end != long_name.end() && *end != '_'; ++end) ;
1188 if (end == long_name.end())
1190 string snap_str(current, end);
1193 for ( ; end != long_name.end() && *end != '_'; ++end) ;
1194 if (end == long_name.end())
1196 string hash_str(current, end);
1199 for ( ; end != long_name.end() && *end != '_'; ++end) ;
1200 if (end == long_name.end())
1202 if (!append_unescaped(current, end, &ns))
1206 for ( ; end != long_name.end() && *end != '_'; ++end) ;
1207 string pstring(current, end);
1209 // Optional generation/shard_id
1210 string genstring, shardstring;
1211 if (end != long_name.end()) {
1213 for ( ; end != long_name.end() && *end != '_'; ++end) ;
1214 if (end == long_name.end())
1216 genstring = string(current, end);
1218 generation = (gen_t)strtoull(genstring.c_str(), NULL, 16);
1221 for ( ; end != long_name.end() && *end != '_'; ++end) ;
1222 if (end != long_name.end())
1224 shardstring = string(current, end);
1226 shard_id = (shard_id_t)strtoul(shardstring.c_str(), NULL, 16);
1229 if (snap_str == "head")
1231 else if (snap_str == "snapdir")
1232 snap = CEPH_SNAPDIR;
1234 snap = strtoull(snap_str.c_str(), NULL, 16);
1235 sscanf(hash_str.c_str(), "%X", &hash);
1237 if (pstring == "none")
1238 pool = (uint64_t)-1;
1240 pool = strtoull(pstring.c_str(), NULL, 16);
1242 (*out) = ghobject_t(hobject_t(name, key, snap, hash, (int64_t)pool, ns), generation, shard_id);
1246 bool LFNIndex::lfn_is_hashed_filename(const string &name)
1248 if (name.size() < (unsigned)FILENAME_SHORT_LEN) {
1251 if (name.substr(name.size() - FILENAME_COOKIE.size(), FILENAME_COOKIE.size())
1252 == FILENAME_COOKIE) {
1259 bool LFNIndex::lfn_must_hash(const string &long_name)
1261 return (int)long_name.size() >= FILENAME_SHORT_LEN;
1264 static inline void buf_to_hex(const unsigned char *buf, int len, char *str)
1268 for (i = 0; i < len; i++) {
1269 sprintf(&str[i*2], "%02x", (int)buf[i]);
1273 int LFNIndex::hash_filename(const char *filename, char *hash, int buf_len)
1275 if (buf_len < FILENAME_HASH_LEN + 1)
1278 char buf[FILENAME_LFN_DIGEST_SIZE];
1279 char hex[FILENAME_LFN_DIGEST_SIZE * 2];
1282 h.Update((const byte *)filename, strlen(filename));
1283 h.Final((byte *)buf);
1285 buf_to_hex((byte *)buf, (FILENAME_HASH_LEN + 1) / 2, hex);
1286 strncpy(hash, hex, FILENAME_HASH_LEN);
1287 hash[FILENAME_HASH_LEN] = '\0';
1291 void LFNIndex::build_filename(const char *old_filename, int i, char *filename, int len)
1293 char hash[FILENAME_HASH_LEN + 1];
1295 assert(len >= FILENAME_SHORT_LEN + 4);
1297 strncpy(filename, old_filename, FILENAME_PREFIX_LEN);
1298 filename[FILENAME_PREFIX_LEN] = '\0';
1299 if ((int)strlen(filename) < FILENAME_PREFIX_LEN)
1301 if (old_filename[FILENAME_PREFIX_LEN] == '\0')
1304 hash_filename(old_filename, hash, sizeof(hash));
1305 int ofs = FILENAME_PREFIX_LEN;
1307 int suffix_len = sprintf(filename + ofs, "_%s_%d_%s", hash, i, FILENAME_COOKIE.c_str());
1308 if (ofs + suffix_len <= FILENAME_SHORT_LEN || !ofs)
1314 bool LFNIndex::short_name_matches(const char *short_name, const char *cand_long_name)
1316 const char *end = short_name;
1318 const char *suffix = end;
1319 if (suffix > short_name) --suffix; // last char
1320 while (suffix > short_name && *suffix != '_') --suffix; // back to first _
1321 if (suffix > short_name) --suffix; // one behind that
1322 while (suffix > short_name && *suffix != '_') --suffix; // back to second _
1325 char buf[FILENAME_SHORT_LEN + 4];
1326 assert((end - suffix) < (int)sizeof(buf));
1327 int r = sscanf(suffix, "_%d_%s", &index, buf);
1330 if (strcmp(buf, FILENAME_COOKIE.c_str()) != 0)
1332 build_filename(cand_long_name, index, buf, sizeof(buf));
1333 return strcmp(short_name, buf) == 0;
1336 string LFNIndex::lfn_get_short_name(const ghobject_t &oid, int i)
1338 string long_name = lfn_generate_object_name(oid);
1339 assert(lfn_must_hash(long_name));
1340 char buf[FILENAME_SHORT_LEN + 4];
1341 build_filename(long_name.c_str(), i, buf, sizeof(buf));
1345 const string &LFNIndex::get_base_path()
1350 string LFNIndex::get_full_path_subdir(const vector<string> &rel)
1352 string retval = get_base_path();
1353 for (vector<string>::const_iterator i = rel.begin();
1357 retval += mangle_path_component(*i);
1362 string LFNIndex::get_full_path(const vector<string> &rel, const string &name)
1364 return get_full_path_subdir(rel) + "/" + name;
1367 string LFNIndex::mangle_path_component(const string &component)
1369 return SUBDIR_PREFIX + component;
1372 string LFNIndex::demangle_path_component(const string &component)
1374 return component.substr(SUBDIR_PREFIX.size(), component.size() - SUBDIR_PREFIX.size());
1377 int LFNIndex::decompose_full_path(const char *in, vector<string> *out,
1378 ghobject_t *oid, string *shortname)
1380 const char *beginning = in + get_base_path().size();
1381 const char *end = beginning;
1385 for ( ; *end != '\0' && *end != '/'; ++end) ;
1387 out->push_back(demangle_path_component(string(beginning, end - beginning)));
1393 *shortname = string(beginning, end - beginning);
1395 int r = lfn_translate(*out, *shortname, oid);
1402 string LFNIndex::mangle_attr_name(const string &attr)
1404 return PHASH_ATTR_PREFIX + attr;