1 /* SPDX-License-Identifier: GPL-2.0-only */
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.
26 * Lot's of the key helpers require a struct ubifs_info *c as the first parameter.
27 * But we are not using it at all currently. That's designed for future extensions of
28 * different c->key_format. But right now, there is only one key type, UBIFS_SIMPLE_KEY_FMT.
31 #ifndef __UBIFS_KEY_H__
32 #define __UBIFS_KEY_H__
35 * key_mask_hash - mask a valid hash value.
36 * @val: value to be masked
38 * We use hash values as offset in directories, so values %0 and %1 are
39 * reserved for "." and "..". %2 is reserved for "end of readdir" marker. This
40 * function makes sure the reserved values are not used.
42 static inline uint32_t key_mask_hash(uint32_t hash)
44 hash &= UBIFS_S_KEY_HASH_MASK;
45 if (unlikely(hash <= 2))
51 * key_r5_hash - R5 hash function (borrowed from reiserfs).
55 static inline uint32_t key_r5_hash(const char *s, int len)
58 const signed char *str = (const signed char *)s;
67 return key_mask_hash(a);
71 * key_test_hash - testing hash function.
75 static inline uint32_t key_test_hash(const char *str, int len)
79 len = min_t(uint32_t, len, 4);
81 return key_mask_hash(a);
85 * ino_key_init - initialize inode key.
86 * @c: UBIFS file-system description object
87 * @key: key to initialize
90 static inline void ino_key_init(const struct ubifs_info *c,
91 union ubifs_key *key, ino_t inum)
94 key->u32[1] = UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS;
98 * ino_key_init_flash - initialize on-flash inode key.
99 * @c: UBIFS file-system description object
100 * @k: key to initialize
101 * @inum: inode number
103 static inline void ino_key_init_flash(const struct ubifs_info *c, void *k,
106 union ubifs_key *key = k;
108 key->j32[0] = cpu_to_le32(inum);
109 key->j32[1] = cpu_to_le32(UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS);
110 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
114 * lowest_ino_key - get the lowest possible inode key.
115 * @c: UBIFS file-system description object
116 * @key: key to initialize
117 * @inum: inode number
119 static inline void lowest_ino_key(const struct ubifs_info *c,
120 union ubifs_key *key, ino_t inum)
127 * highest_ino_key - get the highest possible inode key.
128 * @c: UBIFS file-system description object
129 * @key: key to initialize
130 * @inum: inode number
132 static inline void highest_ino_key(const struct ubifs_info *c,
133 union ubifs_key *key, ino_t inum)
136 key->u32[1] = 0xffffffff;
140 * dent_key_init - initialize directory entry key.
141 * @c: UBIFS file-system description object
142 * @key: key to initialize
143 * @inum: parent inode number
144 * @nm: direntry name and length. Not a string when encrypted!
146 static inline void dent_key_init(const struct ubifs_info *c,
147 union ubifs_key *key, ino_t inum,
148 const struct fscrypt_name *nm)
150 uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
152 ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
153 ubifs_assert(c, !nm->hash && !nm->minor_hash);
155 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
159 * dent_key_init_hash - initialize directory entry key without re-calculating
161 * @c: UBIFS file-system description object
162 * @key: key to initialize
163 * @inum: parent inode number
164 * @hash: direntry name hash
166 static inline void dent_key_init_hash(const struct ubifs_info *c,
167 union ubifs_key *key, ino_t inum,
170 ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
172 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
176 * dent_key_init_flash - initialize on-flash directory entry key.
177 * @c: UBIFS file-system description object
178 * @k: key to initialize
179 * @inum: parent inode number
180 * @nm: direntry name and length
182 static inline void dent_key_init_flash(const struct ubifs_info *c, void *k,
184 const struct fscrypt_name *nm)
186 union ubifs_key *key = k;
187 uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
189 ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
190 key->j32[0] = cpu_to_le32(inum);
191 key->j32[1] = cpu_to_le32(hash |
192 (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS));
193 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
197 * lowest_dent_key - get the lowest possible directory entry key.
198 * @c: UBIFS file-system description object
199 * @key: where to store the lowest key
200 * @inum: parent inode number
202 static inline void lowest_dent_key(const struct ubifs_info *c,
203 union ubifs_key *key, ino_t inum)
206 key->u32[1] = UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS;
210 * xent_key_init - initialize extended attribute entry key.
211 * @c: UBIFS file-system description object
212 * @key: key to initialize
213 * @inum: host inode number
214 * @nm: extended attribute entry name and length
216 static inline void xent_key_init(const struct ubifs_info *c,
217 union ubifs_key *key, ino_t inum,
218 const struct fscrypt_name *nm)
220 uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
222 ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
224 key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
228 * xent_key_init_flash - initialize on-flash extended attribute entry key.
229 * @c: UBIFS file-system description object
230 * @k: key to initialize
231 * @inum: host inode number
232 * @nm: extended attribute entry name and length
234 static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
235 ino_t inum, const struct fscrypt_name *nm)
237 union ubifs_key *key = k;
238 uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
240 ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
241 key->j32[0] = cpu_to_le32(inum);
242 key->j32[1] = cpu_to_le32(hash |
243 (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS));
244 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
248 * lowest_xent_key - get the lowest possible extended attribute entry key.
249 * @c: UBIFS file-system description object
250 * @key: where to store the lowest key
251 * @inum: host inode number
253 static inline void lowest_xent_key(const struct ubifs_info *c,
254 union ubifs_key *key, ino_t inum)
257 key->u32[1] = UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS;
261 * data_key_init - initialize data key.
262 * @c: UBIFS file-system description object
263 * @key: key to initialize
264 * @inum: inode number
265 * @block: block number
267 static inline void data_key_init(const struct ubifs_info *c,
268 union ubifs_key *key, ino_t inum,
271 ubifs_assert(c, !(block & ~UBIFS_S_KEY_BLOCK_MASK));
273 key->u32[1] = block | (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS);
277 * highest_data_key - get the highest possible data key for an inode.
278 * @c: UBIFS file-system description object
279 * @key: key to initialize
280 * @inum: inode number
282 static inline void highest_data_key(const struct ubifs_info *c,
283 union ubifs_key *key, ino_t inum)
285 data_key_init(c, key, inum, UBIFS_S_KEY_BLOCK_MASK);
289 * trun_key_init - initialize truncation node key.
290 * @c: UBIFS file-system description object
291 * @key: key to initialize
292 * @inum: inode number
294 * Note, UBIFS does not have truncation keys on the media and this function is
295 * only used for purposes of replay.
297 static inline void trun_key_init(const struct ubifs_info *c,
298 union ubifs_key *key, ino_t inum)
301 key->u32[1] = UBIFS_TRUN_KEY << UBIFS_S_KEY_BLOCK_BITS;
305 * invalid_key_init - initialize invalid node key.
306 * @c: UBIFS file-system description object
307 * @key: key to initialize
309 * This is a helper function which marks a @key object as invalid.
311 static inline void invalid_key_init(const struct ubifs_info *c,
312 union ubifs_key *key)
314 key->u32[0] = 0xDEADBEAF;
315 key->u32[1] = UBIFS_INVALID_KEY;
319 * key_type - get key type.
320 * @c: UBIFS file-system description object
321 * @key: key to get type of
323 static inline int key_type(const struct ubifs_info *c,
324 const union ubifs_key *key)
326 return key->u32[1] >> UBIFS_S_KEY_BLOCK_BITS;
330 * key_type_flash - get type of a on-flash formatted key.
331 * @c: UBIFS file-system description object
332 * @k: key to get type of
334 static inline int key_type_flash(const struct ubifs_info *c, const void *k)
336 const union ubifs_key *key = k;
338 return le32_to_cpu(key->j32[1]) >> UBIFS_S_KEY_BLOCK_BITS;
342 * key_inum - fetch inode number from key.
343 * @c: UBIFS file-system description object
344 * @k: key to fetch inode number from
346 static inline ino_t key_inum(const struct ubifs_info *c, const void *k)
348 const union ubifs_key *key = k;
354 * key_inum_flash - fetch inode number from an on-flash formatted key.
355 * @c: UBIFS file-system description object
356 * @k: key to fetch inode number from
358 static inline ino_t key_inum_flash(const struct ubifs_info *c, const void *k)
360 const union ubifs_key *key = k;
362 return le32_to_cpu(key->j32[0]);
366 * key_hash - get directory entry hash.
367 * @c: UBIFS file-system description object
368 * @key: the key to get hash from
370 static inline uint32_t key_hash(const struct ubifs_info *c,
371 const union ubifs_key *key)
373 return key->u32[1] & UBIFS_S_KEY_HASH_MASK;
377 * key_hash_flash - get directory entry hash from an on-flash formatted key.
378 * @c: UBIFS file-system description object
379 * @k: the key to get hash from
381 static inline uint32_t key_hash_flash(const struct ubifs_info *c, const void *k)
383 const union ubifs_key *key = k;
385 return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_HASH_MASK;
389 * key_block - get data block number.
390 * @c: UBIFS file-system description object
391 * @key: the key to get the block number from
393 static inline unsigned int key_block(const struct ubifs_info *c,
394 const union ubifs_key *key)
396 return key->u32[1] & UBIFS_S_KEY_BLOCK_MASK;
400 * key_block_flash - get data block number from an on-flash formatted key.
401 * @c: UBIFS file-system description object
402 * @k: the key to get the block number from
404 static inline unsigned int key_block_flash(const struct ubifs_info *c,
407 const union ubifs_key *key = k;
409 return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_BLOCK_MASK;
413 * key_read - transform a key to in-memory format.
414 * @c: UBIFS file-system description object
415 * @from: the key to transform
416 * @to: the key to store the result
418 static inline void key_read(const struct ubifs_info *c, const void *from,
421 const union ubifs_key *f = from;
423 to->u32[0] = le32_to_cpu(f->j32[0]);
424 to->u32[1] = le32_to_cpu(f->j32[1]);
428 * key_write - transform a key from in-memory format.
429 * @c: UBIFS file-system description object
430 * @from: the key to transform
431 * @to: the key to store the result
433 static inline void key_write(const struct ubifs_info *c,
434 const union ubifs_key *from, void *to)
436 union ubifs_key *t = to;
438 t->j32[0] = cpu_to_le32(from->u32[0]);
439 t->j32[1] = cpu_to_le32(from->u32[1]);
440 memset(to + 8, 0, UBIFS_MAX_KEY_LEN - 8);
444 * key_write_idx - transform a key from in-memory format for the index.
445 * @c: UBIFS file-system description object
446 * @from: the key to transform
447 * @to: the key to store the result
449 static inline void key_write_idx(const struct ubifs_info *c,
450 const union ubifs_key *from, void *to)
452 union ubifs_key *t = to;
454 t->j32[0] = cpu_to_le32(from->u32[0]);
455 t->j32[1] = cpu_to_le32(from->u32[1]);
459 * key_copy - copy a key.
460 * @c: UBIFS file-system description object
461 * @from: the key to copy from
462 * @to: the key to copy to
464 static inline void key_copy(const struct ubifs_info *c,
465 const union ubifs_key *from, union ubifs_key *to)
467 to->u64[0] = from->u64[0];
471 * keys_cmp - compare keys.
472 * @c: UBIFS file-system description object
473 * @key1: the first key to compare
474 * @key2: the second key to compare
476 * This function compares 2 keys and returns %-1 if @key1 is less than
477 * @key2, %0 if the keys are equivalent and %1 if @key1 is greater than @key2.
479 static inline int keys_cmp(const struct ubifs_info *c,
480 const union ubifs_key *key1,
481 const union ubifs_key *key2)
483 if (key1->u32[0] < key2->u32[0])
485 if (key1->u32[0] > key2->u32[0])
487 if (key1->u32[1] < key2->u32[1])
489 if (key1->u32[1] > key2->u32[1])
496 * keys_eq - determine if keys are equivalent.
497 * @c: UBIFS file-system description object
498 * @key1: the first key to compare
499 * @key2: the second key to compare
501 * This function compares 2 keys and returns %1 if @key1 is equal to @key2 and
504 static inline int keys_eq(const struct ubifs_info *c,
505 const union ubifs_key *key1,
506 const union ubifs_key *key2)
508 if (key1->u32[0] != key2->u32[0])
510 if (key1->u32[1] != key2->u32[1])
516 * is_hash_key - is a key vulnerable to hash collisions.
517 * @c: UBIFS file-system description object
520 * This function returns %1 if @key is a hashed key or %0 otherwise.
522 static inline int is_hash_key(const struct ubifs_info *c,
523 const union ubifs_key *key)
525 int type = key_type(c, key);
527 return type == UBIFS_DENT_KEY || type == UBIFS_XENT_KEY;
531 * key_max_inode_size - get maximum file size allowed by current key format.
532 * @c: UBIFS file-system description object
534 static inline unsigned long long key_max_inode_size(const struct ubifs_info *c)
536 switch (c->key_fmt) {
537 case UBIFS_SIMPLE_KEY_FMT:
538 return (1ULL << UBIFS_S_KEY_BLOCK_BITS) * UBIFS_BLOCK_SIZE;
544 #endif /* !__UBIFS_KEY_H__ */