From f998bf64ec9d5e3703500176be4a269bbfc416bd Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Wed, 27 Apr 2011 16:32:52 -0700 Subject: [PATCH] lfn: replace hash function for some reason crashes when using libnss --- src/Makefile.am | 2 ++ src/common/ceph_crypto.h | 47 +++++++++++++++++++++++++++++++++++++++- src/cosd.cc | 2 +- src/os/FileStore.cc | 42 ++++++++++++++++++++++++++++------- src/os/FileStore.h | 4 +--- 5 files changed, 84 insertions(+), 13 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 148e24c85ba72..cc270901db739 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -687,6 +687,8 @@ libos_a_SOURCES = \ os/FileJournal.cc \ os/FileStore.cc \ os/JournalingObjectStore.cc +libos_a_CXXFLAGS= ${CRYPTO_CXXFLAGS} ${AM_CXXFLAGS} + libosd_a_SOURCES = \ osd/PG.cc \ diff --git a/src/common/ceph_crypto.h b/src/common/ceph_crypto.h index ebe39b14e51c4..823871962f6d6 100644 --- a/src/common/ceph_crypto.h +++ b/src/common/ceph_crypto.h @@ -5,6 +5,7 @@ #define CEPH_CRYPTO_MD5_DIGESTSIZE 16 #define CEPH_CRYPTO_HMACSHA1_DIGESTSIZE 20 +#define CEPH_CRYPTO_SHA1_DIGESTSIZE 20 #ifdef USE_CRYPTOPP # define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 @@ -18,6 +19,7 @@ namespace ceph { // nothing } using CryptoPP::Weak::MD5; + using CryptoPP::SHA1; class HMACSHA1: public CryptoPP::HMAC { public: @@ -47,7 +49,6 @@ typedef unsigned char byte; namespace ceph { namespace crypto { void init(); - class MD5 { private: PK11Context *ctx; @@ -79,7 +80,51 @@ namespace ceph { Restart(); } }; +#if 0 + class Digest { + private: + PK11Context *ctx; + SECOidTag sec_type; + size_t digest_size; + public: + Digest (SECOidTag _type, size_t _digest_size) : sec_type(_type), digest_size(_digest_size) { + ctx = PK11_CreateDigestContext(_type); + assert(ctx); + Restart(); + } + ~Digest () { + PK11_DestroyContext(ctx, PR_TRUE); + } + void Restart() { + SECStatus s; + s = PK11_DigestBegin(ctx); + assert(s == SECSuccess); + } + void Update (const byte *input, size_t length) { + SECStatus s; + s = PK11_DigestOp(ctx, input, length); + assert(s == SECSuccess); + } + void Final (byte *digest) { + SECStatus s; + unsigned int dummy; + s = PK11_DigestFinal(ctx, digest, &dummy, digest_size); + assert(s == SECSuccess); + assert(dummy == digest_size); + Restart(); + } + }; + class MD5 : public Digest { + public: + MD5 () : Digest(SEC_OID_MD5, CEPH_CRYPTO_MD5_DIGESTSIZE) { } + }; + class SHA1 : public Digest { + public: + SHA1 () : Digest(SEC_OID_SHA1, CEPH_CRYPTO_SHA1_DIGESTSIZE) { } + }; + +#endif class HMACSHA1 { private: PK11SlotInfo *slot; diff --git a/src/cosd.cc b/src/cosd.cc index 4e6fd77632ed9..1287079baa397 100644 --- a/src/cosd.cc +++ b/src/cosd.cc @@ -56,7 +56,7 @@ int main(int argc, const char **argv) vector::iterator args_iter; common_init(args, CEPH_ENTITY_TYPE_OSD, CODE_ENVIRONMENT_DAEMON, 0); - + ceph_heap_profiler_init(); // osd specific args diff --git a/src/os/FileStore.cc b/src/os/FileStore.cc index f6d3045b06e08..af5c9adb04eb3 100644 --- a/src/os/FileStore.cc +++ b/src/os/FileStore.cc @@ -82,6 +82,11 @@ #include +#include "common/ceph_crypto.h" + +using ceph::crypto::MD5; +// using ceph::crypto::SHA1; + /* * long file names will have the following format: * @@ -157,11 +162,29 @@ int sys_listxattr(const char *fn, char *names, size_t len) } #endif +static inline void buf_to_hex(const unsigned char *buf, int len, char *str) +{ + int i; + str[0] = '\0'; + for (i = 0; i < len; i++) { + sprintf(&str[i*2], "%02x", (int)buf[i]); + } +} + static int hash_filename(const char *filename, char *hash, int buf_len) { if (buf_len < FILENAME_HASH_LEN + 1) return -EINVAL; - sprintf(hash, "zzz"); + + char buf[CEPH_CRYPTO_MD5_DIGESTSIZE]; + char hex[CEPH_CRYPTO_MD5_DIGESTSIZE * 2]; + MD5 h; + h.Update((const byte *)filename, strlen(filename)); + h.Final((byte *)buf); + + buf_to_hex((byte *)buf, (FILENAME_HASH_LEN + 1) / 2, hex); + strncpy(hash, hex, FILENAME_HASH_LEN); + hash[FILENAME_HASH_LEN] = '\0'; return 0; } @@ -217,7 +240,7 @@ static void lfn_translate(const char *path, const char *name, char *new_name, in // 012345678901234567890123456789012345678901234567890123456789 // yyyyyyyyyyyyyyyy.zzzzzzzz.a_s -void FileStore::append_oname(const sobject_t &oid, char *s, int len) +int FileStore::append_oname(const sobject_t &oid, char *s, int len) { //assert(sizeof(oid) == 28); char *end = s + len; @@ -239,13 +262,16 @@ void FileStore::append_oname(const sobject_t &oid, char *s, int len) i++; } + int size = t - s; + if (oid.snap == CEPH_NOSNAP) - snprintf(t, end - t, "_head"); + size += snprintf(t, end - t, "_head"); else if (oid.snap == CEPH_SNAPDIR) - snprintf(t, end - t, "_snapdir"); + size += snprintf(t, end - t, "_snapdir"); else - snprintf(t, end - t, "_%llx", (long long unsigned)oid.snap); - //parse_object(t+1); + size += snprintf(t, end - t, "_%llx", (long long unsigned)oid.snap); + + return size; } bool FileStore::parse_object(char *s, sobject_t& o) @@ -306,9 +332,9 @@ int FileStore::lfn_get(coll_t cid, const sobject_t& oid, char *pathname, int len filename = pathname + path_len; *lfn = '\0'; - append_oname(oid, lfn, lfn_len); + int actual_len = append_oname(oid, lfn, lfn_len); - if (oid.oid.name.size() < FILENAME_PREFIX_LEN) { + if (actual_len < (int)FILENAME_PREFIX_LEN) { /* not a long file name, just build it as it is */ strncpy(filename, lfn, len - path_len); *is_lfn = 0; diff --git a/src/os/FileStore.h b/src/os/FileStore.h index 59c37e26e0921..663849fc5b6cf 100644 --- a/src/os/FileStore.h +++ b/src/os/FileStore.h @@ -66,10 +66,8 @@ class FileStore : public JournalingObjectStore { Finisher ondisk_finisher; // helper fns - void append_oname(const sobject_t &oid, char *s, int len); - //void get_oname(sobject_t oid, char *s); + int append_oname(const sobject_t &oid, char *s, int len); int get_cdir(coll_t cid, char *s, int len); - void get_coname(coll_t cid, const sobject_t& oid, char *s, int len); bool parse_object(char *s, sobject_t& o); int lock_fsid(); -- 2.47.3