1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation.
7 * Authors: Artem Bityutskiy (Битюцкий Артём)
12 * This header contains various key-related definitions and helper function.
13 * UBIFS allows several key schemes, so we access key fields only via these
14 * helpers. At the moment only one key scheme is supported.
19 * Keys are 64-bits long. First 32-bits are inode number (parent inode number
20 * in case of direntry key). Next 3 bits are node type. The last 29 bits are
21 * 4KiB offset in case of inode node, and direntry hash in case of a direntry
22 * node. We use "r5" hash borrowed from reiserfs.
25 #ifndef __UBIFS_KEY_H__
26 #define __UBIFS_KEY_H__
29 * key_mask_hash - mask a valid hash value.
30 * @val: value to be masked
32 * We use hash values as offset in directories, so values %0 and %1 are
33 * reserved for "." and "..". %2 is reserved for "end of readdir" marker. This
34 * function makes sure the reserved values are not used.
36 static inline uint32_t key_mask_hash(uint32_t hash)
38 hash &= UBIFS_S_KEY_HASH_MASK;
39 if (unlikely(hash <= 2))
45 * key_r5_hash - R5 hash function (borrowed from reiserfs).
49 static inline uint32_t key_r5_hash(const char *s, int len)
52 const signed char *str = (const signed char *)s;
61 return key_mask_hash(a);
65 * key_test_hash - testing hash function.
69 static inline uint32_t key_test_hash(const char *str, int len)
73 len = min_t(uint32_t, len, 4);
75 return key_mask_hash(a);
79 * ino_key_init - initialize inode key.
80 * @c: UBIFS file-system description object
81 * @key: key to initialize
84 static inline void ino_key_init(const struct ubifs_info *c,
85 union ubifs_key *key, ino_t inum)
88 key->u32[1] = UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS;
92 * ino_key_init_flash - initialize on-flash inode key.
93 * @c: UBIFS file-system description object
94 * @k: key to initialize
97 static inline void ino_key_init_flash(const struct ubifs_info *c, void *k,
100 union ubifs_key *key = k;
102 key->j32[0] = cpu_to_le32(inum);
103 key->j32[1] = cpu_to_le32(UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS);
104 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
108 * lowest_ino_key - get the lowest possible inode key.
109 * @c: UBIFS file-system description object
110 * @key: key to initialize
111 * @inum: inode number
113 static inline void lowest_ino_key(const struct ubifs_info *c,
114 union ubifs_key *key, ino_t inum)
121 * highest_ino_key - get the highest possible inode key.
122 * @c: UBIFS file-system description object
123 * @key: key to initialize
124 * @inum: inode number
126 static inline void highest_ino_key(const struct ubifs_info *c,
127 union ubifs_key *key, ino_t inum)
130 key->u32[1] = 0xffffffff;
134 * dent_key_init - initialize directory entry key.
135 * @c: UBIFS file-system description object
136 * @key: key to initialize
137 * @inum: parent inode number
138 * @nm: direntry name and length
140 static inline void dent_key_init(const struct ubifs_info *c,
141 union ubifs_key *key, ino_t inum,
142 const struct qstr *nm)
144 uint32_t hash = c->key_hash(nm->name, nm->len);
146 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
148 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
152 * dent_key_init_hash - initialize directory entry key without re-calculating
154 * @c: UBIFS file-system description object
155 * @key: key to initialize
156 * @inum: parent inode number
157 * @hash: direntry name hash
159 static inline void dent_key_init_hash(const struct ubifs_info *c,
160 union ubifs_key *key, ino_t inum,
163 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
165 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
169 * dent_key_init_flash - initialize on-flash directory entry key.
170 * @c: UBIFS file-system description object
171 * @k: key to initialize
172 * @inum: parent inode number
173 * @nm: direntry name and length
175 static inline void dent_key_init_flash(const struct ubifs_info *c, void *k,
176 ino_t inum, const struct qstr *nm)
178 union ubifs_key *key = k;
179 uint32_t hash = c->key_hash(nm->name, nm->len);
181 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
182 key->j32[0] = cpu_to_le32(inum);
183 key->j32[1] = cpu_to_le32(hash |
184 (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS));
185 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
189 * lowest_dent_key - get the lowest possible directory entry key.
190 * @c: UBIFS file-system description object
191 * @key: where to store the lowest key
192 * @inum: parent inode number
194 static inline void lowest_dent_key(const struct ubifs_info *c,
195 union ubifs_key *key, ino_t inum)
198 key->u32[1] = UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS;
202 * xent_key_init - initialize extended attribute entry key.
203 * @c: UBIFS file-system description object
204 * @key: key to initialize
205 * @inum: host inode number
206 * @nm: extended attribute entry name and length
208 static inline void xent_key_init(const struct ubifs_info *c,
209 union ubifs_key *key, ino_t inum,
210 const struct qstr *nm)
212 uint32_t hash = c->key_hash(nm->name, nm->len);
214 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
216 key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
220 * xent_key_init_flash - initialize on-flash extended attribute entry key.
221 * @c: UBIFS file-system description object
222 * @k: key to initialize
223 * @inum: host inode number
224 * @nm: extended attribute entry name and length
226 static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
227 ino_t inum, const struct qstr *nm)
229 union ubifs_key *key = k;
230 uint32_t hash = c->key_hash(nm->name, nm->len);
232 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
233 key->j32[0] = cpu_to_le32(inum);
234 key->j32[1] = cpu_to_le32(hash |
235 (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS));
236 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
240 * lowest_xent_key - get the lowest possible extended attribute entry key.
241 * @c: UBIFS file-system description object
242 * @key: where to store the lowest key
243 * @inum: host inode number
245 static inline void lowest_xent_key(const struct ubifs_info *c,
246 union ubifs_key *key, ino_t inum)
249 key->u32[1] = UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS;
253 * data_key_init - initialize data key.
254 * @c: UBIFS file-system description object
255 * @key: key to initialize
256 * @inum: inode number
257 * @block: block number
259 static inline void data_key_init(const struct ubifs_info *c,
260 union ubifs_key *key, ino_t inum,
263 ubifs_assert(!(block & ~UBIFS_S_KEY_BLOCK_MASK));
265 key->u32[1] = block | (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS);
269 * highest_data_key - get the highest possible data key for an inode.
270 * @c: UBIFS file-system description object
271 * @key: key to initialize
272 * @inum: inode number
274 static inline void highest_data_key(const struct ubifs_info *c,
275 union ubifs_key *key, ino_t inum)
277 data_key_init(c, key, inum, UBIFS_S_KEY_BLOCK_MASK);
281 * trun_key_init - initialize truncation node key.
282 * @c: UBIFS file-system description object
283 * @key: key to initialize
284 * @inum: inode number
286 * Note, UBIFS does not have truncation keys on the media and this function is
287 * only used for purposes of replay.
289 static inline void trun_key_init(const struct ubifs_info *c,
290 union ubifs_key *key, ino_t inum)
293 key->u32[1] = UBIFS_TRUN_KEY << UBIFS_S_KEY_BLOCK_BITS;
297 * invalid_key_init - initialize invalid node key.
298 * @c: UBIFS file-system description object
299 * @key: key to initialize
301 * This is a helper function which marks a @key object as invalid.
303 static inline void invalid_key_init(const struct ubifs_info *c,
304 union ubifs_key *key)
306 key->u32[0] = 0xDEADBEAF;
307 key->u32[1] = UBIFS_INVALID_KEY;
311 * key_type - get key type.
312 * @c: UBIFS file-system description object
313 * @key: key to get type of
315 static inline int key_type(const struct ubifs_info *c,
316 const union ubifs_key *key)
318 return key->u32[1] >> UBIFS_S_KEY_BLOCK_BITS;
322 * key_type_flash - get type of a on-flash formatted key.
323 * @c: UBIFS file-system description object
324 * @k: key to get type of
326 static inline int key_type_flash(const struct ubifs_info *c, const void *k)
328 const union ubifs_key *key = k;
330 return le32_to_cpu(key->j32[1]) >> UBIFS_S_KEY_BLOCK_BITS;
334 * key_inum - fetch inode number from key.
335 * @c: UBIFS file-system description object
336 * @k: key to fetch inode number from
338 static inline ino_t key_inum(const struct ubifs_info *c, const void *k)
340 const union ubifs_key *key = k;
346 * key_inum_flash - fetch inode number from an on-flash formatted key.
347 * @c: UBIFS file-system description object
348 * @k: key to fetch inode number from
350 static inline ino_t key_inum_flash(const struct ubifs_info *c, const void *k)
352 const union ubifs_key *key = k;
354 return le32_to_cpu(key->j32[0]);
358 * key_hash - get directory entry hash.
359 * @c: UBIFS file-system description object
360 * @key: the key to get hash from
362 static inline uint32_t key_hash(const struct ubifs_info *c,
363 const union ubifs_key *key)
365 return key->u32[1] & UBIFS_S_KEY_HASH_MASK;
369 * key_hash_flash - get directory entry hash from an on-flash formatted key.
370 * @c: UBIFS file-system description object
371 * @k: the key to get hash from
373 static inline uint32_t key_hash_flash(const struct ubifs_info *c, const void *k)
375 const union ubifs_key *key = k;
377 return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_HASH_MASK;
381 * key_block - get data block number.
382 * @c: UBIFS file-system description object
383 * @key: the key to get the block number from
385 static inline unsigned int key_block(const struct ubifs_info *c,
386 const union ubifs_key *key)
388 return key->u32[1] & UBIFS_S_KEY_BLOCK_MASK;
392 * key_block_flash - get data block number from an on-flash formatted key.
393 * @c: UBIFS file-system description object
394 * @k: the key to get the block number from
396 static inline unsigned int key_block_flash(const struct ubifs_info *c,
399 const union ubifs_key *key = k;
401 return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_BLOCK_MASK;
405 * key_read - transform a key to in-memory format.
406 * @c: UBIFS file-system description object
407 * @from: the key to transform
408 * @to: the key to store the result
410 static inline void key_read(const struct ubifs_info *c, const void *from,
413 const union ubifs_key *f = from;
415 to->u32[0] = le32_to_cpu(f->j32[0]);
416 to->u32[1] = le32_to_cpu(f->j32[1]);
420 * key_write - transform a key from in-memory format.
421 * @c: UBIFS file-system description object
422 * @from: the key to transform
423 * @to: the key to store the result
425 static inline void key_write(const struct ubifs_info *c,
426 const union ubifs_key *from, void *to)
428 union ubifs_key *t = to;
430 t->j32[0] = cpu_to_le32(from->u32[0]);
431 t->j32[1] = cpu_to_le32(from->u32[1]);
432 memset(to + 8, 0, UBIFS_MAX_KEY_LEN - 8);
436 * key_write_idx - transform a key from in-memory format for the index.
437 * @c: UBIFS file-system description object
438 * @from: the key to transform
439 * @to: the key to store the result
441 static inline void key_write_idx(const struct ubifs_info *c,
442 const union ubifs_key *from, void *to)
444 union ubifs_key *t = to;
446 t->j32[0] = cpu_to_le32(from->u32[0]);
447 t->j32[1] = cpu_to_le32(from->u32[1]);
451 * key_copy - copy a key.
452 * @c: UBIFS file-system description object
453 * @from: the key to copy from
454 * @to: the key to copy to
456 static inline void key_copy(const struct ubifs_info *c,
457 const union ubifs_key *from, union ubifs_key *to)
459 to->u64[0] = from->u64[0];
463 * keys_cmp - compare keys.
464 * @c: UBIFS file-system description object
465 * @key1: the first key to compare
466 * @key2: the second key to compare
468 * This function compares 2 keys and returns %-1 if @key1 is less than
469 * @key2, %0 if the keys are equivalent and %1 if @key1 is greater than @key2.
471 static inline int keys_cmp(const struct ubifs_info *c,
472 const union ubifs_key *key1,
473 const union ubifs_key *key2)
475 if (key1->u32[0] < key2->u32[0])
477 if (key1->u32[0] > key2->u32[0])
479 if (key1->u32[1] < key2->u32[1])
481 if (key1->u32[1] > key2->u32[1])
488 * keys_eq - determine if keys are equivalent.
489 * @c: UBIFS file-system description object
490 * @key1: the first key to compare
491 * @key2: the second key to compare
493 * This function compares 2 keys and returns %1 if @key1 is equal to @key2 and
496 static inline int keys_eq(const struct ubifs_info *c,
497 const union ubifs_key *key1,
498 const union ubifs_key *key2)
500 if (key1->u32[0] != key2->u32[0])
502 if (key1->u32[1] != key2->u32[1])
508 * is_hash_key - is a key vulnerable to hash collisions.
509 * @c: UBIFS file-system description object
512 * This function returns %1 if @key is a hashed key or %0 otherwise.
514 static inline int is_hash_key(const struct ubifs_info *c,
515 const union ubifs_key *key)
517 int type = key_type(c, key);
519 return type == UBIFS_DENT_KEY || type == UBIFS_XENT_KEY;
523 * key_max_inode_size - get maximum file size allowed by current key format.
524 * @c: UBIFS file-system description object
526 static inline unsigned long long key_max_inode_size(const struct ubifs_info *c)
528 switch (c->key_fmt) {
529 case UBIFS_SIMPLE_KEY_FMT:
530 return (1ULL << UBIFS_S_KEY_BLOCK_BITS) * UBIFS_BLOCK_SIZE;
536 #endif /* !__UBIFS_KEY_H__ */