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.
37 #ifndef __UBIFS_KEY_H__
38 #define __UBIFS_KEY_H__
41 * key_mask_hash - mask a valid hash value.
42 * @val: value to be masked
44 * We use hash values as offset in directories, so values %0 and %1 are
45 * reserved for "." and "..". %2 is reserved for "end of readdir" marker. This
46 * function makes sure the reserved values are not used.
48 static inline uint32_t key_mask_hash(uint32_t hash)
50 hash &= UBIFS_S_KEY_HASH_MASK;
51 if (unlikely(hash <= 2))
57 * key_r5_hash - R5 hash function (borrowed from reiserfs).
61 static inline uint32_t key_r5_hash(const char *s, int len)
64 const signed char *str = (const signed char *)s;
73 return key_mask_hash(a);
77 * key_test_hash - testing hash function.
81 static inline uint32_t key_test_hash(const char *str, int len)
85 len = min_t(uint32_t, len, 4);
87 return key_mask_hash(a);
91 * ino_key_init - initialize inode key.
92 * @c: UBIFS file-system description object
93 * @key: key to initialize
96 static inline void ino_key_init(const struct ubifs_info *c,
97 union ubifs_key *key, ino_t inum)
100 key->u32[1] = UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS;
104 * ino_key_init_flash - initialize on-flash inode key.
105 * @c: UBIFS file-system description object
106 * @k: key to initialize
107 * @inum: inode number
109 static inline void ino_key_init_flash(const struct ubifs_info *c, void *k,
112 union ubifs_key *key = k;
114 key->j32[0] = cpu_to_le32(inum);
115 key->j32[1] = cpu_to_le32(UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS);
116 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
120 * lowest_ino_key - get the lowest possible inode key.
121 * @c: UBIFS file-system description object
122 * @key: key to initialize
123 * @inum: inode number
125 static inline void lowest_ino_key(const struct ubifs_info *c,
126 union ubifs_key *key, ino_t inum)
133 * highest_ino_key - get the highest possible inode key.
134 * @c: UBIFS file-system description object
135 * @key: key to initialize
136 * @inum: inode number
138 static inline void highest_ino_key(const struct ubifs_info *c,
139 union ubifs_key *key, ino_t inum)
142 key->u32[1] = 0xffffffff;
146 * dent_key_init - initialize directory entry key.
147 * @c: UBIFS file-system description object
148 * @key: key to initialize
149 * @inum: parent inode number
150 * @nm: direntry name and length
152 static inline void dent_key_init(const struct ubifs_info *c,
153 union ubifs_key *key, ino_t inum,
154 const struct qstr *nm)
156 uint32_t hash = c->key_hash(nm->name, nm->len);
158 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
160 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
164 * dent_key_init_hash - initialize directory entry key without re-calculating
166 * @c: UBIFS file-system description object
167 * @key: key to initialize
168 * @inum: parent inode number
169 * @hash: direntry name hash
171 static inline void dent_key_init_hash(const struct ubifs_info *c,
172 union ubifs_key *key, ino_t inum,
175 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
177 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
181 * dent_key_init_flash - initialize on-flash directory entry key.
182 * @c: UBIFS file-system description object
183 * @k: key to initialize
184 * @inum: parent inode number
185 * @nm: direntry name and length
187 static inline void dent_key_init_flash(const struct ubifs_info *c, void *k,
188 ino_t inum, const struct qstr *nm)
190 union ubifs_key *key = k;
191 uint32_t hash = c->key_hash(nm->name, nm->len);
193 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
194 key->j32[0] = cpu_to_le32(inum);
195 key->j32[1] = cpu_to_le32(hash |
196 (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS));
197 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
201 * lowest_dent_key - get the lowest possible directory entry key.
202 * @c: UBIFS file-system description object
203 * @key: where to store the lowest key
204 * @inum: parent inode number
206 static inline void lowest_dent_key(const struct ubifs_info *c,
207 union ubifs_key *key, ino_t inum)
210 key->u32[1] = UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS;
214 * xent_key_init - initialize extended attribute entry key.
215 * @c: UBIFS file-system description object
216 * @key: key to initialize
217 * @inum: host inode number
218 * @nm: extended attribute entry name and length
220 static inline void xent_key_init(const struct ubifs_info *c,
221 union ubifs_key *key, ino_t inum,
222 const struct qstr *nm)
224 uint32_t hash = c->key_hash(nm->name, nm->len);
226 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
228 key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
232 * xent_key_init_hash - initialize extended attribute entry key without
233 * re-calculating hash function.
234 * @c: UBIFS file-system description object
235 * @key: key to initialize
236 * @inum: host inode number
237 * @hash: extended attribute entry name hash
239 static inline void xent_key_init_hash(const struct ubifs_info *c,
240 union ubifs_key *key, ino_t inum,
243 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
245 key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
249 * xent_key_init_flash - initialize on-flash extended attribute entry key.
250 * @c: UBIFS file-system description object
251 * @k: key to initialize
252 * @inum: host inode number
253 * @nm: extended attribute entry name and length
255 static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
256 ino_t inum, const struct qstr *nm)
258 union ubifs_key *key = k;
259 uint32_t hash = c->key_hash(nm->name, nm->len);
261 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
262 key->j32[0] = cpu_to_le32(inum);
263 key->j32[1] = cpu_to_le32(hash |
264 (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS));
265 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
269 * lowest_xent_key - get the lowest possible extended attribute entry key.
270 * @c: UBIFS file-system description object
271 * @key: where to store the lowest key
272 * @inum: host inode number
274 static inline void lowest_xent_key(const struct ubifs_info *c,
275 union ubifs_key *key, ino_t inum)
278 key->u32[1] = UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS;
282 * data_key_init - initialize data key.
283 * @c: UBIFS file-system description object
284 * @key: key to initialize
285 * @inum: inode number
286 * @block: block number
288 static inline void data_key_init(const struct ubifs_info *c,
289 union ubifs_key *key, ino_t inum,
292 ubifs_assert(!(block & ~UBIFS_S_KEY_BLOCK_MASK));
294 key->u32[1] = block | (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS);
298 * data_key_init_flash - initialize on-flash data key.
299 * @c: UBIFS file-system description object
300 * @k: key to initialize
301 * @inum: inode number
302 * @block: block number
304 static inline void data_key_init_flash(const struct ubifs_info *c, void *k,
305 ino_t inum, unsigned int block)
307 union ubifs_key *key = k;
309 ubifs_assert(!(block & ~UBIFS_S_KEY_BLOCK_MASK));
310 key->j32[0] = cpu_to_le32(inum);
311 key->j32[1] = cpu_to_le32(block |
312 (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS));
313 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
317 * trun_key_init - initialize truncation node key.
318 * @c: UBIFS file-system description object
319 * @key: key to initialize
320 * @inum: inode number
322 * Note, UBIFS does not have truncation keys on the media and this function is
323 * only used for purposes of replay.
325 static inline void trun_key_init(const struct ubifs_info *c,
326 union ubifs_key *key, ino_t inum)
329 key->u32[1] = UBIFS_TRUN_KEY << UBIFS_S_KEY_BLOCK_BITS;
333 * key_type - get key type.
334 * @c: UBIFS file-system description object
335 * @key: key to get type of
337 static inline int key_type(const struct ubifs_info *c,
338 const union ubifs_key *key)
340 return key->u32[1] >> UBIFS_S_KEY_BLOCK_BITS;
344 * key_type_flash - get type of a on-flash formatted key.
345 * @c: UBIFS file-system description object
346 * @k: key to get type of
348 static inline int key_type_flash(const struct ubifs_info *c, const void *k)
350 const union ubifs_key *key = k;
352 return le32_to_cpu(key->j32[1]) >> UBIFS_S_KEY_BLOCK_BITS;
356 * key_inum - fetch inode number from key.
357 * @c: UBIFS file-system description object
358 * @k: key to fetch inode number from
360 static inline ino_t key_inum(const struct ubifs_info *c, const void *k)
362 const union ubifs_key *key = k;
368 * key_inum_flash - fetch inode number from an on-flash formatted key.
369 * @c: UBIFS file-system description object
370 * @k: key to fetch inode number from
372 static inline ino_t key_inum_flash(const struct ubifs_info *c, const void *k)
374 const union ubifs_key *key = k;
376 return le32_to_cpu(key->j32[0]);
380 * key_hash - get directory entry hash.
381 * @c: UBIFS file-system description object
382 * @key: the key to get hash from
384 static inline uint32_t key_hash(const struct ubifs_info *c,
385 const union ubifs_key *key)
387 return key->u32[1] & UBIFS_S_KEY_HASH_MASK;
391 * key_hash_flash - get directory entry hash from an on-flash formatted key.
392 * @c: UBIFS file-system description object
393 * @k: the key to get hash from
395 static inline uint32_t key_hash_flash(const struct ubifs_info *c, const void *k)
397 const union ubifs_key *key = k;
399 return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_HASH_MASK;
403 * key_block - get data block number.
404 * @c: UBIFS file-system description object
405 * @key: the key to get the block number from
407 static inline unsigned int key_block(const struct ubifs_info *c,
408 const union ubifs_key *key)
410 return key->u32[1] & UBIFS_S_KEY_BLOCK_MASK;
414 * key_block_flash - get data block number from an on-flash formatted key.
415 * @c: UBIFS file-system description object
416 * @k: the key to get the block number from
418 static inline unsigned int key_block_flash(const struct ubifs_info *c,
421 const union ubifs_key *key = k;
423 return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_BLOCK_MASK;
427 * key_read - transform a key to in-memory format.
428 * @c: UBIFS file-system description object
429 * @from: the key to transform
430 * @to: the key to store the result
432 static inline void key_read(const struct ubifs_info *c, const void *from,
435 const union ubifs_key *f = from;
437 to->u32[0] = le32_to_cpu(f->j32[0]);
438 to->u32[1] = le32_to_cpu(f->j32[1]);
442 * key_write - transform a key from in-memory format.
443 * @c: UBIFS file-system description object
444 * @from: the key to transform
445 * @to: the key to store the result
447 static inline void key_write(const struct ubifs_info *c,
448 const union ubifs_key *from, void *to)
450 union ubifs_key *t = to;
452 t->j32[0] = cpu_to_le32(from->u32[0]);
453 t->j32[1] = cpu_to_le32(from->u32[1]);
454 memset(to + 8, 0, UBIFS_MAX_KEY_LEN - 8);
458 * key_write_idx - transform a key from in-memory format for the index.
459 * @c: UBIFS file-system description object
460 * @from: the key to transform
461 * @to: the key to store the result
463 static inline void key_write_idx(const struct ubifs_info *c,
464 const union ubifs_key *from, void *to)
466 union ubifs_key *t = to;
468 t->j32[0] = cpu_to_le32(from->u32[0]);
469 t->j32[1] = cpu_to_le32(from->u32[1]);
473 * key_copy - copy a key.
474 * @c: UBIFS file-system description object
475 * @from: the key to copy from
476 * @to: the key to copy to
478 static inline void key_copy(const struct ubifs_info *c,
479 const union ubifs_key *from, union ubifs_key *to)
481 to->u64[0] = from->u64[0];
485 * keys_cmp - compare keys.
486 * @c: UBIFS file-system description object
487 * @key1: the first key to compare
488 * @key2: the second key to compare
490 * This function compares 2 keys and returns %-1 if @key1 is less than
491 * @key2, %0 if the keys are equivalent and %1 if @key1 is greater than @key2.
493 static inline int keys_cmp(const struct ubifs_info *c,
494 const union ubifs_key *key1,
495 const union ubifs_key *key2)
497 if (key1->u32[0] < key2->u32[0])
499 if (key1->u32[0] > key2->u32[0])
501 if (key1->u32[1] < key2->u32[1])
503 if (key1->u32[1] > key2->u32[1])
510 * keys_eq - determine if keys are equivalent.
511 * @c: UBIFS file-system description object
512 * @key1: the first key to compare
513 * @key2: the second key to compare
515 * This function compares 2 keys and returns %1 if @key1 is equal to @key2 and
518 static inline int keys_eq(const struct ubifs_info *c,
519 const union ubifs_key *key1,
520 const union ubifs_key *key2)
522 if (key1->u32[0] != key2->u32[0])
524 if (key1->u32[1] != key2->u32[1])
530 * is_hash_key - is a key vulnerable to hash collisions.
531 * @c: UBIFS file-system description object
534 * This function returns %1 if @key is a hashed key or %0 otherwise.
536 static inline int is_hash_key(const struct ubifs_info *c,
537 const union ubifs_key *key)
539 int type = key_type(c, key);
541 return type == UBIFS_DENT_KEY || type == UBIFS_XENT_KEY;
545 * key_max_inode_size - get maximum file size allowed by current key format.
546 * @c: UBIFS file-system description object
548 static inline unsigned long long key_max_inode_size(const struct ubifs_info *c)
550 switch (c->key_fmt) {
551 case UBIFS_SIMPLE_KEY_FMT:
552 return (1ULL << UBIFS_S_KEY_BLOCK_BITS) * UBIFS_BLOCK_SIZE;
557 #endif /* !__UBIFS_KEY_H__ */