^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * This file is part of UBIFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2006-2008 Nokia Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Authors: Artem Bityutskiy (Битюцкий Артём)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Adrian Hunter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * This header contains various key-related definitions and helper function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * UBIFS allows several key schemes, so we access key fields only via these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * helpers. At the moment only one key scheme is supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Simple key scheme
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * ~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Keys are 64-bits long. First 32-bits are inode number (parent inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * in case of direntry key). Next 3 bits are node type. The last 29 bits are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * 4KiB offset in case of inode node, and direntry hash in case of a direntry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * node. We use "r5" hash borrowed from reiserfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Lot's of the key helpers require a struct ubifs_info *c as the first parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * But we are not using it at all currently. That's designed for future extensions of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * different c->key_format. But right now, there is only one key type, UBIFS_SIMPLE_KEY_FMT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #ifndef __UBIFS_KEY_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define __UBIFS_KEY_H__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * key_mask_hash - mask a valid hash value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @val: value to be masked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * We use hash values as offset in directories, so values %0 and %1 are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * reserved for "." and "..". %2 is reserved for "end of readdir" marker. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * function makes sure the reserved values are not used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static inline uint32_t key_mask_hash(uint32_t hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) hash &= UBIFS_S_KEY_HASH_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (unlikely(hash <= 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) hash += 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * key_r5_hash - R5 hash function (borrowed from reiserfs).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * @s: direntry name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * @len: name length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static inline uint32_t key_r5_hash(const char *s, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) uint32_t a = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) const signed char *str = (const signed char *)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) while (len--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) a += *str << 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) a += *str >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) a *= 11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) str++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return key_mask_hash(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * key_test_hash - testing hash function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * @str: direntry name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * @len: name length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static inline uint32_t key_test_hash(const char *str, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) uint32_t a = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) len = min_t(uint32_t, len, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) memcpy(&a, str, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return key_mask_hash(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * ino_key_init - initialize inode key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * @key: key to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * @inum: inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static inline void ino_key_init(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) union ubifs_key *key, ino_t inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) key->u32[0] = inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) key->u32[1] = UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * ino_key_init_flash - initialize on-flash inode key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * @k: key to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * @inum: inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static inline void ino_key_init_flash(const struct ubifs_info *c, void *k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ino_t inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) union ubifs_key *key = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) key->j32[0] = cpu_to_le32(inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) key->j32[1] = cpu_to_le32(UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * lowest_ino_key - get the lowest possible inode key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * @key: key to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * @inum: inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static inline void lowest_ino_key(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) union ubifs_key *key, ino_t inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) key->u32[0] = inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) key->u32[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * highest_ino_key - get the highest possible inode key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * @key: key to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * @inum: inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static inline void highest_ino_key(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) union ubifs_key *key, ino_t inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) key->u32[0] = inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) key->u32[1] = 0xffffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * dent_key_init - initialize directory entry key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * @key: key to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * @inum: parent inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * @nm: direntry name and length. Not a string when encrypted!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static inline void dent_key_init(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) union ubifs_key *key, ino_t inum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) const struct fscrypt_name *nm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) key->u32[0] = inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * dent_key_init_hash - initialize directory entry key without re-calculating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * hash function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * @key: key to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * @inum: parent inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * @hash: direntry name hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static inline void dent_key_init_hash(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) union ubifs_key *key, ino_t inum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) uint32_t hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) key->u32[0] = inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * dent_key_init_flash - initialize on-flash directory entry key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * @k: key to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * @inum: parent inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * @nm: direntry name and length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static inline void dent_key_init_flash(const struct ubifs_info *c, void *k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) ino_t inum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) const struct fscrypt_name *nm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) union ubifs_key *key = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) key->j32[0] = cpu_to_le32(inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) key->j32[1] = cpu_to_le32(hash |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * lowest_dent_key - get the lowest possible directory entry key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @key: where to store the lowest key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * @inum: parent inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static inline void lowest_dent_key(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) union ubifs_key *key, ino_t inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) key->u32[0] = inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) key->u32[1] = UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * xent_key_init - initialize extended attribute entry key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * @key: key to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * @inum: host inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * @nm: extended attribute entry name and length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static inline void xent_key_init(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) union ubifs_key *key, ino_t inum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) const struct fscrypt_name *nm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) key->u32[0] = inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * xent_key_init_flash - initialize on-flash extended attribute entry key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * @k: key to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * @inum: host inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * @nm: extended attribute entry name and length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ino_t inum, const struct fscrypt_name *nm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) union ubifs_key *key = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) key->j32[0] = cpu_to_le32(inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) key->j32[1] = cpu_to_le32(hash |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * lowest_xent_key - get the lowest possible extended attribute entry key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * @key: where to store the lowest key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * @inum: host inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static inline void lowest_xent_key(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) union ubifs_key *key, ino_t inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) key->u32[0] = inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) key->u32[1] = UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * data_key_init - initialize data key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * @key: key to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * @inum: inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * @block: block number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static inline void data_key_init(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) union ubifs_key *key, ino_t inum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) unsigned int block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) ubifs_assert(c, !(block & ~UBIFS_S_KEY_BLOCK_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) key->u32[0] = inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) key->u32[1] = block | (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * highest_data_key - get the highest possible data key for an inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * @key: key to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * @inum: inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static inline void highest_data_key(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) union ubifs_key *key, ino_t inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) data_key_init(c, key, inum, UBIFS_S_KEY_BLOCK_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * trun_key_init - initialize truncation node key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * @key: key to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * @inum: inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * Note, UBIFS does not have truncation keys on the media and this function is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * only used for purposes of replay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static inline void trun_key_init(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) union ubifs_key *key, ino_t inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) key->u32[0] = inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) key->u32[1] = UBIFS_TRUN_KEY << UBIFS_S_KEY_BLOCK_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * invalid_key_init - initialize invalid node key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * @key: key to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * This is a helper function which marks a @key object as invalid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static inline void invalid_key_init(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) union ubifs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) key->u32[0] = 0xDEADBEAF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) key->u32[1] = UBIFS_INVALID_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * key_type - get key type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * @key: key to get type of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static inline int key_type(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) const union ubifs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return key->u32[1] >> UBIFS_S_KEY_BLOCK_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * key_type_flash - get type of a on-flash formatted key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * @k: key to get type of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static inline int key_type_flash(const struct ubifs_info *c, const void *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) const union ubifs_key *key = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return le32_to_cpu(key->j32[1]) >> UBIFS_S_KEY_BLOCK_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * key_inum - fetch inode number from key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * @k: key to fetch inode number from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static inline ino_t key_inum(const struct ubifs_info *c, const void *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) const union ubifs_key *key = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return key->u32[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * key_inum_flash - fetch inode number from an on-flash formatted key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * @k: key to fetch inode number from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static inline ino_t key_inum_flash(const struct ubifs_info *c, const void *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) const union ubifs_key *key = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return le32_to_cpu(key->j32[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * key_hash - get directory entry hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * @key: the key to get hash from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static inline uint32_t key_hash(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) const union ubifs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return key->u32[1] & UBIFS_S_KEY_HASH_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * key_hash_flash - get directory entry hash from an on-flash formatted key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * @k: the key to get hash from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static inline uint32_t key_hash_flash(const struct ubifs_info *c, const void *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) const union ubifs_key *key = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_HASH_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * key_block - get data block number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * @key: the key to get the block number from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static inline unsigned int key_block(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) const union ubifs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return key->u32[1] & UBIFS_S_KEY_BLOCK_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * key_block_flash - get data block number from an on-flash formatted key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * @k: the key to get the block number from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static inline unsigned int key_block_flash(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) const void *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) const union ubifs_key *key = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_BLOCK_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * key_read - transform a key to in-memory format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * @from: the key to transform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * @to: the key to store the result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) static inline void key_read(const struct ubifs_info *c, const void *from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) union ubifs_key *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) const union ubifs_key *f = from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) to->u32[0] = le32_to_cpu(f->j32[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) to->u32[1] = le32_to_cpu(f->j32[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * key_write - transform a key from in-memory format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * @from: the key to transform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * @to: the key to store the result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static inline void key_write(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) const union ubifs_key *from, void *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) union ubifs_key *t = to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) t->j32[0] = cpu_to_le32(from->u32[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) t->j32[1] = cpu_to_le32(from->u32[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) memset(to + 8, 0, UBIFS_MAX_KEY_LEN - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * key_write_idx - transform a key from in-memory format for the index.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * @from: the key to transform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * @to: the key to store the result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) static inline void key_write_idx(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) const union ubifs_key *from, void *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) union ubifs_key *t = to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) t->j32[0] = cpu_to_le32(from->u32[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) t->j32[1] = cpu_to_le32(from->u32[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * key_copy - copy a key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * @from: the key to copy from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * @to: the key to copy to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) static inline void key_copy(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) const union ubifs_key *from, union ubifs_key *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) to->u64[0] = from->u64[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * keys_cmp - compare keys.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * @key1: the first key to compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * @key2: the second key to compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * This function compares 2 keys and returns %-1 if @key1 is less than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * @key2, %0 if the keys are equivalent and %1 if @key1 is greater than @key2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) static inline int keys_cmp(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) const union ubifs_key *key1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) const union ubifs_key *key2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (key1->u32[0] < key2->u32[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (key1->u32[0] > key2->u32[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (key1->u32[1] < key2->u32[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (key1->u32[1] > key2->u32[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) * keys_eq - determine if keys are equivalent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) * @key1: the first key to compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) * @key2: the second key to compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * This function compares 2 keys and returns %1 if @key1 is equal to @key2 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * %0 if not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) static inline int keys_eq(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) const union ubifs_key *key1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) const union ubifs_key *key2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (key1->u32[0] != key2->u32[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) if (key1->u32[1] != key2->u32[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * is_hash_key - is a key vulnerable to hash collisions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * @key: key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) * This function returns %1 if @key is a hashed key or %0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) static inline int is_hash_key(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) const union ubifs_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) int type = key_type(c, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) return type == UBIFS_DENT_KEY || type == UBIFS_XENT_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * key_max_inode_size - get maximum file size allowed by current key format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) static inline unsigned long long key_max_inode_size(const struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) switch (c->key_fmt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) case UBIFS_SIMPLE_KEY_FMT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return (1ULL << UBIFS_S_KEY_BLOCK_BITS) * UBIFS_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) #endif /* !__UBIFS_KEY_H__ */