123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- #ifndef __UBIFS_KEY_H__
- #define __UBIFS_KEY_H__
- static inline uint32_t key_mask_hash(uint32_t hash)
- {
- hash &= UBIFS_S_KEY_HASH_MASK;
- if (unlikely(hash <= 2))
- hash += 3;
- return hash;
- }
- static inline uint32_t key_r5_hash(const char *s, int len)
- {
- uint32_t a = 0;
- const signed char *str = (const signed char *)s;
- while (*str) {
- a += *str << 4;
- a += *str >> 4;
- a *= 11;
- str++;
- }
- return key_mask_hash(a);
- }
- static inline uint32_t key_test_hash(const char *str, int len)
- {
- uint32_t a = 0;
- len = min_t(uint32_t, len, 4);
- memcpy(&a, str, len);
- return key_mask_hash(a);
- }
- static inline void ino_key_init(const struct ubifs_info *c,
- union ubifs_key *key, ino_t inum)
- {
- key->u32[0] = inum;
- key->u32[1] = UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS;
- }
- static inline void ino_key_init_flash(const struct ubifs_info *c, void *k,
- ino_t inum)
- {
- union ubifs_key *key = k;
- key->j32[0] = cpu_to_le32(inum);
- key->j32[1] = cpu_to_le32(UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS);
- memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
- }
- static inline void lowest_ino_key(const struct ubifs_info *c,
- union ubifs_key *key, ino_t inum)
- {
- key->u32[0] = inum;
- key->u32[1] = 0;
- }
- static inline void highest_ino_key(const struct ubifs_info *c,
- union ubifs_key *key, ino_t inum)
- {
- key->u32[0] = inum;
- key->u32[1] = 0xffffffff;
- }
- static inline void dent_key_init(const struct ubifs_info *c,
- union ubifs_key *key, ino_t inum,
- const struct qstr *nm)
- {
- uint32_t hash = c->key_hash(nm->name, nm->len);
- ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
- key->u32[0] = inum;
- key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
- }
- static inline void dent_key_init_hash(const struct ubifs_info *c,
- union ubifs_key *key, ino_t inum,
- uint32_t hash)
- {
- ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
- key->u32[0] = inum;
- key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
- }
- static inline void dent_key_init_flash(const struct ubifs_info *c, void *k,
- ino_t inum, const struct qstr *nm)
- {
- union ubifs_key *key = k;
- uint32_t hash = c->key_hash(nm->name, nm->len);
- ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
- key->j32[0] = cpu_to_le32(inum);
- key->j32[1] = cpu_to_le32(hash |
- (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS));
- memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
- }
- static inline void lowest_dent_key(const struct ubifs_info *c,
- union ubifs_key *key, ino_t inum)
- {
- key->u32[0] = inum;
- key->u32[1] = UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS;
- }
- static inline void xent_key_init(const struct ubifs_info *c,
- union ubifs_key *key, ino_t inum,
- const struct qstr *nm)
- {
- uint32_t hash = c->key_hash(nm->name, nm->len);
- ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
- key->u32[0] = inum;
- key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
- }
- static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
- ino_t inum, const struct qstr *nm)
- {
- union ubifs_key *key = k;
- uint32_t hash = c->key_hash(nm->name, nm->len);
- ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
- key->j32[0] = cpu_to_le32(inum);
- key->j32[1] = cpu_to_le32(hash |
- (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS));
- memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
- }
- static inline void lowest_xent_key(const struct ubifs_info *c,
- union ubifs_key *key, ino_t inum)
- {
- key->u32[0] = inum;
- key->u32[1] = UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS;
- }
- static inline void data_key_init(const struct ubifs_info *c,
- union ubifs_key *key, ino_t inum,
- unsigned int block)
- {
- ubifs_assert(!(block & ~UBIFS_S_KEY_BLOCK_MASK));
- key->u32[0] = inum;
- key->u32[1] = block | (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS);
- }
- static inline void highest_data_key(const struct ubifs_info *c,
- union ubifs_key *key, ino_t inum)
- {
- data_key_init(c, key, inum, UBIFS_S_KEY_BLOCK_MASK);
- }
- static inline void trun_key_init(const struct ubifs_info *c,
- union ubifs_key *key, ino_t inum)
- {
- key->u32[0] = inum;
- key->u32[1] = UBIFS_TRUN_KEY << UBIFS_S_KEY_BLOCK_BITS;
- }
- static inline void invalid_key_init(const struct ubifs_info *c,
- union ubifs_key *key)
- {
- key->u32[0] = 0xDEADBEAF;
- key->u32[1] = UBIFS_INVALID_KEY;
- }
- static inline int key_type(const struct ubifs_info *c,
- const union ubifs_key *key)
- {
- return key->u32[1] >> UBIFS_S_KEY_BLOCK_BITS;
- }
- static inline int key_type_flash(const struct ubifs_info *c, const void *k)
- {
- const union ubifs_key *key = k;
- return le32_to_cpu(key->j32[1]) >> UBIFS_S_KEY_BLOCK_BITS;
- }
- static inline ino_t key_inum(const struct ubifs_info *c, const void *k)
- {
- const union ubifs_key *key = k;
- return key->u32[0];
- }
- static inline ino_t key_inum_flash(const struct ubifs_info *c, const void *k)
- {
- const union ubifs_key *key = k;
- return le32_to_cpu(key->j32[0]);
- }
- static inline uint32_t key_hash(const struct ubifs_info *c,
- const union ubifs_key *key)
- {
- return key->u32[1] & UBIFS_S_KEY_HASH_MASK;
- }
- static inline uint32_t key_hash_flash(const struct ubifs_info *c, const void *k)
- {
- const union ubifs_key *key = k;
- return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_HASH_MASK;
- }
- static inline unsigned int key_block(const struct ubifs_info *c,
- const union ubifs_key *key)
- {
- return key->u32[1] & UBIFS_S_KEY_BLOCK_MASK;
- }
- static inline unsigned int key_block_flash(const struct ubifs_info *c,
- const void *k)
- {
- const union ubifs_key *key = k;
- return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_BLOCK_MASK;
- }
- static inline void key_read(const struct ubifs_info *c, const void *from,
- union ubifs_key *to)
- {
- const union ubifs_key *f = from;
- to->u32[0] = le32_to_cpu(f->j32[0]);
- to->u32[1] = le32_to_cpu(f->j32[1]);
- }
- static inline void key_write(const struct ubifs_info *c,
- const union ubifs_key *from, void *to)
- {
- union ubifs_key *t = to;
- t->j32[0] = cpu_to_le32(from->u32[0]);
- t->j32[1] = cpu_to_le32(from->u32[1]);
- memset(to + 8, 0, UBIFS_MAX_KEY_LEN - 8);
- }
- static inline void key_write_idx(const struct ubifs_info *c,
- const union ubifs_key *from, void *to)
- {
- union ubifs_key *t = to;
- t->j32[0] = cpu_to_le32(from->u32[0]);
- t->j32[1] = cpu_to_le32(from->u32[1]);
- }
- static inline void key_copy(const struct ubifs_info *c,
- const union ubifs_key *from, union ubifs_key *to)
- {
- to->u64[0] = from->u64[0];
- }
- static inline int keys_cmp(const struct ubifs_info *c,
- const union ubifs_key *key1,
- const union ubifs_key *key2)
- {
- if (key1->u32[0] < key2->u32[0])
- return -1;
- if (key1->u32[0] > key2->u32[0])
- return 1;
- if (key1->u32[1] < key2->u32[1])
- return -1;
- if (key1->u32[1] > key2->u32[1])
- return 1;
- return 0;
- }
- static inline int keys_eq(const struct ubifs_info *c,
- const union ubifs_key *key1,
- const union ubifs_key *key2)
- {
- if (key1->u32[0] != key2->u32[0])
- return 0;
- if (key1->u32[1] != key2->u32[1])
- return 0;
- return 1;
- }
- static inline int is_hash_key(const struct ubifs_info *c,
- const union ubifs_key *key)
- {
- int type = key_type(c, key);
- return type == UBIFS_DENT_KEY || type == UBIFS_XENT_KEY;
- }
- static inline unsigned long long key_max_inode_size(const struct ubifs_info *c)
- {
- switch (c->key_fmt) {
- case UBIFS_SIMPLE_KEY_FMT:
- return (1ULL << UBIFS_S_KEY_BLOCK_BITS) * UBIFS_BLOCK_SIZE;
- default:
- return 0;
- }
- }
- #endif
|