Sage Weil | 0dee3c2 | 2009-10-06 11:31:06 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Some non-inline ceph helpers |
| 3 | */ |
| 4 | #include "types.h" |
| 5 | |
| 6 | int ceph_flags_to_mode(int flags) |
| 7 | { |
| 8 | #ifdef O_DIRECTORY /* fixme */ |
| 9 | if ((flags & O_DIRECTORY) == O_DIRECTORY) |
| 10 | return CEPH_FILE_MODE_PIN; |
| 11 | #endif |
| 12 | #ifdef O_LAZY |
| 13 | if (flags & O_LAZY) |
| 14 | return CEPH_FILE_MODE_LAZY; |
| 15 | #endif |
| 16 | if ((flags & O_APPEND) == O_APPEND) |
| 17 | flags |= O_WRONLY; |
| 18 | |
| 19 | flags &= O_ACCMODE; |
| 20 | if ((flags & O_RDWR) == O_RDWR) |
| 21 | return CEPH_FILE_MODE_RDWR; |
| 22 | if ((flags & O_WRONLY) == O_WRONLY) |
| 23 | return CEPH_FILE_MODE_WR; |
| 24 | return CEPH_FILE_MODE_RD; |
| 25 | } |
| 26 | |
| 27 | int ceph_caps_for_mode(int mode) |
| 28 | { |
| 29 | switch (mode) { |
| 30 | case CEPH_FILE_MODE_PIN: |
| 31 | return CEPH_CAP_PIN; |
| 32 | case CEPH_FILE_MODE_RD: |
| 33 | return CEPH_CAP_PIN | CEPH_CAP_FILE_SHARED | |
| 34 | CEPH_CAP_FILE_RD | CEPH_CAP_FILE_CACHE; |
| 35 | case CEPH_FILE_MODE_RDWR: |
| 36 | return CEPH_CAP_PIN | CEPH_CAP_FILE_SHARED | |
| 37 | CEPH_CAP_FILE_EXCL | |
| 38 | CEPH_CAP_FILE_RD | CEPH_CAP_FILE_CACHE | |
| 39 | CEPH_CAP_FILE_WR | CEPH_CAP_FILE_BUFFER | |
| 40 | CEPH_CAP_AUTH_SHARED | CEPH_CAP_AUTH_EXCL | |
| 41 | CEPH_CAP_XATTR_SHARED | CEPH_CAP_XATTR_EXCL; |
| 42 | case CEPH_FILE_MODE_WR: |
| 43 | return CEPH_CAP_PIN | CEPH_CAP_FILE_SHARED | |
| 44 | CEPH_CAP_FILE_EXCL | |
| 45 | CEPH_CAP_FILE_WR | CEPH_CAP_FILE_BUFFER | |
| 46 | CEPH_CAP_AUTH_SHARED | CEPH_CAP_AUTH_EXCL | |
| 47 | CEPH_CAP_XATTR_SHARED | CEPH_CAP_XATTR_EXCL; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | /* Name hashing routines. Initial hash value */ |
| 53 | /* Hash courtesy of the R5 hash in reiserfs modulo sign bits */ |
| 54 | #define ceph_init_name_hash() 0 |
| 55 | |
| 56 | /* partial hash update function. Assume roughly 4 bits per character */ |
| 57 | static unsigned long ceph_partial_name_hash(unsigned long c, |
| 58 | unsigned long prevhash) |
| 59 | { |
| 60 | return (prevhash + (c << 4) + (c >> 4)) * 11; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Finally: cut down the number of bits to a int value (and try to avoid |
| 65 | * losing bits) |
| 66 | */ |
| 67 | static unsigned long ceph_end_name_hash(unsigned long hash) |
| 68 | { |
| 69 | return hash & 0xffffffff; |
| 70 | } |
| 71 | |
| 72 | /* Compute the hash for a name string. */ |
| 73 | unsigned int ceph_full_name_hash(const char *name, unsigned int len) |
| 74 | { |
| 75 | unsigned long hash = ceph_init_name_hash(); |
| 76 | while (len--) |
| 77 | hash = ceph_partial_name_hash(*name++, hash); |
| 78 | return ceph_end_name_hash(hash); |
| 79 | } |
| 80 | |