2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
24 * This header contains various key-related definitions and helper function.
25 * UBIFS allows several key schemes, so we access key fields only via these
26 * helpers. At the moment only one key scheme is supported.
31 * Keys are 64-bits long. First 32-bits are inode number (parent inode number
32 * in case of direntry key). Next 3 bits are node type. The last 29 bits are
33 * 4KiB offset in case of inode node, and direntry hash in case of a direntry
34 * node. We use "r5" hash borrowed from reiserfs.
38 * Lot's of the key helpers require a struct ubifs_info *c as the first parameter.
39 * But we are not using it at all currently. That's designed for future extensions of
40 * different c->key_format. But right now, there is only one key type, UBIFS_SIMPLE_KEY_FMT.
43 #ifndef __UBIFS_KEY_H__
44 #define __UBIFS_KEY_H__
47 * key_mask_hash - mask a valid hash value.
48 * @val: value to be masked
50 * We use hash values as offset in directories, so values %0 and %1 are
51 * reserved for "." and "..". %2 is reserved for "end of readdir" marker. This
52 * function makes sure the reserved values are not used.
54 static inline uint32_t key_mask_hash(uint32_t hash
)
56 hash
&= UBIFS_S_KEY_HASH_MASK
;
57 if (unlikely(hash
<= 2))
63 * key_r5_hash - R5 hash function (borrowed from reiserfs).
67 static inline uint32_t key_r5_hash(const char *s
, int len
)
70 const signed char *str
= (const signed char *)s
;
79 return key_mask_hash(a
);
83 * key_test_hash - testing hash function.
87 static inline uint32_t key_test_hash(const char *str
, int len
)
91 len
= min_t(uint32_t, len
, 4);
93 return key_mask_hash(a
);
97 * ino_key_init - initialize inode key.
98 * @c: UBIFS file-system description object
99 * @key: key to initialize
100 * @inum: inode number
102 static inline void ino_key_init(const struct ubifs_info
*c
,
103 union ubifs_key
*key
, ino_t inum
)
106 key
->u32
[1] = UBIFS_INO_KEY
<< UBIFS_S_KEY_BLOCK_BITS
;
110 * ino_key_init_flash - initialize on-flash inode key.
111 * @c: UBIFS file-system description object
112 * @k: key to initialize
113 * @inum: inode number
115 static inline void ino_key_init_flash(const struct ubifs_info
*c
, void *k
,
118 union ubifs_key
*key
= k
;
120 key
->j32
[0] = cpu_to_le32(inum
);
121 key
->j32
[1] = cpu_to_le32(UBIFS_INO_KEY
<< UBIFS_S_KEY_BLOCK_BITS
);
122 memset(k
+ 8, 0, UBIFS_MAX_KEY_LEN
- 8);
126 * lowest_ino_key - get the lowest possible inode key.
127 * @c: UBIFS file-system description object
128 * @key: key to initialize
129 * @inum: inode number
131 static inline void lowest_ino_key(const struct ubifs_info
*c
,
132 union ubifs_key
*key
, ino_t inum
)
139 * highest_ino_key - get the highest possible inode key.
140 * @c: UBIFS file-system description object
141 * @key: key to initialize
142 * @inum: inode number
144 static inline void highest_ino_key(const struct ubifs_info
*c
,
145 union ubifs_key
*key
, ino_t inum
)
148 key
->u32
[1] = 0xffffffff;
152 * dent_key_init - initialize directory entry key.
153 * @c: UBIFS file-system description object
154 * @key: key to initialize
155 * @inum: parent inode number
156 * @nm: direntry name and length. Not a string when encrypted!
158 static inline void dent_key_init(const struct ubifs_info
*c
,
159 union ubifs_key
*key
, ino_t inum
,
160 const struct fscrypt_name
*nm
)
162 uint32_t hash
= c
->key_hash(fname_name(nm
), fname_len(nm
));
164 ubifs_assert(!(hash
& ~UBIFS_S_KEY_HASH_MASK
));
166 key
->u32
[1] = hash
| (UBIFS_DENT_KEY
<< UBIFS_S_KEY_HASH_BITS
);
170 * dent_key_init_hash - initialize directory entry key without re-calculating
172 * @c: UBIFS file-system description object
173 * @key: key to initialize
174 * @inum: parent inode number
175 * @hash: direntry name hash
177 static inline void dent_key_init_hash(const struct ubifs_info
*c
,
178 union ubifs_key
*key
, ino_t inum
,
181 ubifs_assert(!(hash
& ~UBIFS_S_KEY_HASH_MASK
));
183 key
->u32
[1] = hash
| (UBIFS_DENT_KEY
<< UBIFS_S_KEY_HASH_BITS
);
187 * dent_key_init_flash - initialize on-flash directory entry key.
188 * @c: UBIFS file-system description object
189 * @k: key to initialize
190 * @inum: parent inode number
191 * @nm: direntry name and length
193 static inline void dent_key_init_flash(const struct ubifs_info
*c
, void *k
,
195 const struct fscrypt_name
*nm
)
197 union ubifs_key
*key
= k
;
198 uint32_t hash
= c
->key_hash(fname_name(nm
), fname_len(nm
));
200 ubifs_assert(!(hash
& ~UBIFS_S_KEY_HASH_MASK
));
201 key
->j32
[0] = cpu_to_le32(inum
);
202 key
->j32
[1] = cpu_to_le32(hash
|
203 (UBIFS_DENT_KEY
<< UBIFS_S_KEY_HASH_BITS
));
204 memset(k
+ 8, 0, UBIFS_MAX_KEY_LEN
- 8);
208 * lowest_dent_key - get the lowest possible directory entry key.
209 * @c: UBIFS file-system description object
210 * @key: where to store the lowest key
211 * @inum: parent inode number
213 static inline void lowest_dent_key(const struct ubifs_info
*c
,
214 union ubifs_key
*key
, ino_t inum
)
217 key
->u32
[1] = UBIFS_DENT_KEY
<< UBIFS_S_KEY_HASH_BITS
;
221 * xent_key_init - initialize extended attribute entry key.
222 * @c: UBIFS file-system description object
223 * @key: key to initialize
224 * @inum: host inode number
225 * @nm: extended attribute entry name and length
227 static inline void xent_key_init(const struct ubifs_info
*c
,
228 union ubifs_key
*key
, ino_t inum
,
229 const struct fscrypt_name
*nm
)
231 uint32_t hash
= c
->key_hash(fname_name(nm
), fname_len(nm
));
233 ubifs_assert(!(hash
& ~UBIFS_S_KEY_HASH_MASK
));
235 key
->u32
[1] = hash
| (UBIFS_XENT_KEY
<< UBIFS_S_KEY_HASH_BITS
);
239 * xent_key_init_flash - initialize on-flash extended attribute entry key.
240 * @c: UBIFS file-system description object
241 * @k: key to initialize
242 * @inum: host inode number
243 * @nm: extended attribute entry name and length
245 static inline void xent_key_init_flash(const struct ubifs_info
*c
, void *k
,
246 ino_t inum
, const struct fscrypt_name
*nm
)
248 union ubifs_key
*key
= k
;
249 uint32_t hash
= c
->key_hash(fname_name(nm
), fname_len(nm
));
251 ubifs_assert(!(hash
& ~UBIFS_S_KEY_HASH_MASK
));
252 key
->j32
[0] = cpu_to_le32(inum
);
253 key
->j32
[1] = cpu_to_le32(hash
|
254 (UBIFS_XENT_KEY
<< UBIFS_S_KEY_HASH_BITS
));
255 memset(k
+ 8, 0, UBIFS_MAX_KEY_LEN
- 8);
259 * lowest_xent_key - get the lowest possible extended attribute entry key.
260 * @c: UBIFS file-system description object
261 * @key: where to store the lowest key
262 * @inum: host inode number
264 static inline void lowest_xent_key(const struct ubifs_info
*c
,
265 union ubifs_key
*key
, ino_t inum
)
268 key
->u32
[1] = UBIFS_XENT_KEY
<< UBIFS_S_KEY_HASH_BITS
;
272 * data_key_init - initialize data key.
273 * @c: UBIFS file-system description object
274 * @key: key to initialize
275 * @inum: inode number
276 * @block: block number
278 static inline void data_key_init(const struct ubifs_info
*c
,
279 union ubifs_key
*key
, ino_t inum
,
282 ubifs_assert(!(block
& ~UBIFS_S_KEY_BLOCK_MASK
));
284 key
->u32
[1] = block
| (UBIFS_DATA_KEY
<< UBIFS_S_KEY_BLOCK_BITS
);
288 * highest_data_key - get the highest possible data key for an inode.
289 * @c: UBIFS file-system description object
290 * @key: key to initialize
291 * @inum: inode number
293 static inline void highest_data_key(const struct ubifs_info
*c
,
294 union ubifs_key
*key
, ino_t inum
)
296 data_key_init(c
, key
, inum
, UBIFS_S_KEY_BLOCK_MASK
);
300 * trun_key_init - initialize truncation node key.
301 * @c: UBIFS file-system description object
302 * @key: key to initialize
303 * @inum: inode number
305 * Note, UBIFS does not have truncation keys on the media and this function is
306 * only used for purposes of replay.
308 static inline void trun_key_init(const struct ubifs_info
*c
,
309 union ubifs_key
*key
, ino_t inum
)
312 key
->u32
[1] = UBIFS_TRUN_KEY
<< UBIFS_S_KEY_BLOCK_BITS
;
316 * invalid_key_init - initialize invalid node key.
317 * @c: UBIFS file-system description object
318 * @key: key to initialize
320 * This is a helper function which marks a @key object as invalid.
322 static inline void invalid_key_init(const struct ubifs_info
*c
,
323 union ubifs_key
*key
)
325 key
->u32
[0] = 0xDEADBEAF;
326 key
->u32
[1] = UBIFS_INVALID_KEY
;
330 * key_type - get key type.
331 * @c: UBIFS file-system description object
332 * @key: key to get type of
334 static inline int key_type(const struct ubifs_info
*c
,
335 const union ubifs_key
*key
)
337 return key
->u32
[1] >> UBIFS_S_KEY_BLOCK_BITS
;
341 * key_type_flash - get type of a on-flash formatted key.
342 * @c: UBIFS file-system description object
343 * @k: key to get type of
345 static inline int key_type_flash(const struct ubifs_info
*c
, const void *k
)
347 const union ubifs_key
*key
= k
;
349 return le32_to_cpu(key
->j32
[1]) >> UBIFS_S_KEY_BLOCK_BITS
;
353 * key_inum - fetch inode number from key.
354 * @c: UBIFS file-system description object
355 * @k: key to fetch inode number from
357 static inline ino_t
key_inum(const struct ubifs_info
*c
, const void *k
)
359 const union ubifs_key
*key
= k
;
365 * key_inum_flash - fetch inode number from an on-flash formatted key.
366 * @c: UBIFS file-system description object
367 * @k: key to fetch inode number from
369 static inline ino_t
key_inum_flash(const struct ubifs_info
*c
, const void *k
)
371 const union ubifs_key
*key
= k
;
373 return le32_to_cpu(key
->j32
[0]);
377 * key_hash - get directory entry hash.
378 * @c: UBIFS file-system description object
379 * @key: the key to get hash from
381 static inline uint32_t key_hash(const struct ubifs_info
*c
,
382 const union ubifs_key
*key
)
384 return key
->u32
[1] & UBIFS_S_KEY_HASH_MASK
;
388 * key_hash_flash - get directory entry hash from an on-flash formatted key.
389 * @c: UBIFS file-system description object
390 * @k: the key to get hash from
392 static inline uint32_t key_hash_flash(const struct ubifs_info
*c
, const void *k
)
394 const union ubifs_key
*key
= k
;
396 return le32_to_cpu(key
->j32
[1]) & UBIFS_S_KEY_HASH_MASK
;
400 * key_block - get data block number.
401 * @c: UBIFS file-system description object
402 * @key: the key to get the block number from
404 static inline unsigned int key_block(const struct ubifs_info
*c
,
405 const union ubifs_key
*key
)
407 return key
->u32
[1] & UBIFS_S_KEY_BLOCK_MASK
;
411 * key_block_flash - get data block number from an on-flash formatted key.
412 * @c: UBIFS file-system description object
413 * @k: the key to get the block number from
415 static inline unsigned int key_block_flash(const struct ubifs_info
*c
,
418 const union ubifs_key
*key
= k
;
420 return le32_to_cpu(key
->j32
[1]) & UBIFS_S_KEY_BLOCK_MASK
;
424 * key_read - transform a key to in-memory format.
425 * @c: UBIFS file-system description object
426 * @from: the key to transform
427 * @to: the key to store the result
429 static inline void key_read(const struct ubifs_info
*c
, const void *from
,
432 const union ubifs_key
*f
= from
;
434 to
->u32
[0] = le32_to_cpu(f
->j32
[0]);
435 to
->u32
[1] = le32_to_cpu(f
->j32
[1]);
439 * key_write - transform a key from in-memory format.
440 * @c: UBIFS file-system description object
441 * @from: the key to transform
442 * @to: the key to store the result
444 static inline void key_write(const struct ubifs_info
*c
,
445 const union ubifs_key
*from
, void *to
)
447 union ubifs_key
*t
= to
;
449 t
->j32
[0] = cpu_to_le32(from
->u32
[0]);
450 t
->j32
[1] = cpu_to_le32(from
->u32
[1]);
451 memset(to
+ 8, 0, UBIFS_MAX_KEY_LEN
- 8);
455 * key_write_idx - transform a key from in-memory format for the index.
456 * @c: UBIFS file-system description object
457 * @from: the key to transform
458 * @to: the key to store the result
460 static inline void key_write_idx(const struct ubifs_info
*c
,
461 const union ubifs_key
*from
, void *to
)
463 union ubifs_key
*t
= to
;
465 t
->j32
[0] = cpu_to_le32(from
->u32
[0]);
466 t
->j32
[1] = cpu_to_le32(from
->u32
[1]);
470 * key_copy - copy a key.
471 * @c: UBIFS file-system description object
472 * @from: the key to copy from
473 * @to: the key to copy to
475 static inline void key_copy(const struct ubifs_info
*c
,
476 const union ubifs_key
*from
, union ubifs_key
*to
)
478 to
->u64
[0] = from
->u64
[0];
482 * keys_cmp - compare keys.
483 * @c: UBIFS file-system description object
484 * @key1: the first key to compare
485 * @key2: the second key to compare
487 * This function compares 2 keys and returns %-1 if @key1 is less than
488 * @key2, %0 if the keys are equivalent and %1 if @key1 is greater than @key2.
490 static inline int keys_cmp(const struct ubifs_info
*c
,
491 const union ubifs_key
*key1
,
492 const union ubifs_key
*key2
)
494 if (key1
->u32
[0] < key2
->u32
[0])
496 if (key1
->u32
[0] > key2
->u32
[0])
498 if (key1
->u32
[1] < key2
->u32
[1])
500 if (key1
->u32
[1] > key2
->u32
[1])
507 * keys_eq - determine if keys are equivalent.
508 * @c: UBIFS file-system description object
509 * @key1: the first key to compare
510 * @key2: the second key to compare
512 * This function compares 2 keys and returns %1 if @key1 is equal to @key2 and
515 static inline int keys_eq(const struct ubifs_info
*c
,
516 const union ubifs_key
*key1
,
517 const union ubifs_key
*key2
)
519 if (key1
->u32
[0] != key2
->u32
[0])
521 if (key1
->u32
[1] != key2
->u32
[1])
527 * is_hash_key - is a key vulnerable to hash collisions.
528 * @c: UBIFS file-system description object
531 * This function returns %1 if @key is a hashed key or %0 otherwise.
533 static inline int is_hash_key(const struct ubifs_info
*c
,
534 const union ubifs_key
*key
)
536 int type
= key_type(c
, key
);
538 return type
== UBIFS_DENT_KEY
|| type
== UBIFS_XENT_KEY
;
542 * key_max_inode_size - get maximum file size allowed by current key format.
543 * @c: UBIFS file-system description object
545 static inline unsigned long long key_max_inode_size(const struct ubifs_info
*c
)
547 switch (c
->key_fmt
) {
548 case UBIFS_SIMPLE_KEY_FMT
:
549 return (1ULL << UBIFS_S_KEY_BLOCK_BITS
) * UBIFS_BLOCK_SIZE
;
555 #endif /* !__UBIFS_KEY_H__ */