Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
key.h
Go to the documentation of this file.
1 /*
2  * This file is part of UBIFS.
3  *
4  * Copyright (C) 2006-2008 Nokia Corporation.
5  *
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.
9  *
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
13  * more details.
14  *
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
18  *
19  * Authors: Artem Bityutskiy (Битюцкий Артём)
20  * Adrian Hunter
21  */
22 
23 /*
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.
27  *
28  * Simple key scheme
29  * ~~~~~~~~~~~~~~~~~
30  *
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.
35  */
36 
37 #ifndef __UBIFS_KEY_H__
38 #define __UBIFS_KEY_H__
39 
48 static inline uint32_t key_mask_hash(uint32_t hash)
49 {
50  hash &= UBIFS_S_KEY_HASH_MASK;
51  if (unlikely(hash <= 2))
52  hash += 3;
53  return hash;
54 }
55 
61 static inline uint32_t key_r5_hash(const char *s, int len)
62 {
63  uint32_t a = 0;
64  const signed char *str = (const signed char *)s;
65 
66  while (*str) {
67  a += *str << 4;
68  a += *str >> 4;
69  a *= 11;
70  str++;
71  }
72 
73  return key_mask_hash(a);
74 }
75 
81 static inline uint32_t key_test_hash(const char *str, int len)
82 {
83  uint32_t a = 0;
84 
85  len = min_t(uint32_t, len, 4);
86  memcpy(&a, str, len);
87  return key_mask_hash(a);
88 }
89 
96 static inline void ino_key_init(const struct ubifs_info *c,
97  union ubifs_key *key, ino_t inum)
98 {
99  key->u32[0] = inum;
101 }
102 
109 static inline void ino_key_init_flash(const struct ubifs_info *c, void *k,
110  ino_t inum)
111 {
112  union ubifs_key *key = k;
113 
114  key->j32[0] = cpu_to_le32(inum);
116  memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
117 }
118 
125 static inline void lowest_ino_key(const struct ubifs_info *c,
126  union ubifs_key *key, ino_t inum)
127 {
128  key->u32[0] = inum;
129  key->u32[1] = 0;
130 }
131 
138 static inline void highest_ino_key(const struct ubifs_info *c,
139  union ubifs_key *key, ino_t inum)
140 {
141  key->u32[0] = inum;
142  key->u32[1] = 0xffffffff;
143 }
144 
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)
155 {
156  uint32_t hash = c->key_hash(nm->name, nm->len);
157 
159  key->u32[0] = inum;
160  key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
161 }
162 
171 static inline void dent_key_init_hash(const struct ubifs_info *c,
172  union ubifs_key *key, ino_t inum,
173  uint32_t hash)
174 {
176  key->u32[0] = inum;
177  key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
178 }
179 
187 static inline void dent_key_init_flash(const struct ubifs_info *c, void *k,
188  ino_t inum, const struct qstr *nm)
189 {
190  union ubifs_key *key = k;
191  uint32_t hash = c->key_hash(nm->name, nm->len);
192 
194  key->j32[0] = cpu_to_le32(inum);
195  key->j32[1] = cpu_to_le32(hash |
197  memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
198 }
199 
206 static inline void lowest_dent_key(const struct ubifs_info *c,
207  union ubifs_key *key, ino_t inum)
208 {
209  key->u32[0] = inum;
211 }
212 
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)
223 {
224  uint32_t hash = c->key_hash(nm->name, nm->len);
225 
227  key->u32[0] = inum;
228  key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
229 }
230 
238 static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
239  ino_t inum, const struct qstr *nm)
240 {
241  union ubifs_key *key = k;
242  uint32_t hash = c->key_hash(nm->name, nm->len);
243 
245  key->j32[0] = cpu_to_le32(inum);
246  key->j32[1] = cpu_to_le32(hash |
248  memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
249 }
250 
257 static inline void lowest_xent_key(const struct ubifs_info *c,
258  union ubifs_key *key, ino_t inum)
259 {
260  key->u32[0] = inum;
262 }
263 
271 static inline void data_key_init(const struct ubifs_info *c,
272  union ubifs_key *key, ino_t inum,
273  unsigned int block)
274 {
276  key->u32[0] = inum;
277  key->u32[1] = block | (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS);
278 }
279 
286 static inline void highest_data_key(const struct ubifs_info *c,
287  union ubifs_key *key, ino_t inum)
288 {
289  data_key_init(c, key, inum, UBIFS_S_KEY_BLOCK_MASK);
290 }
291 
301 static inline void trun_key_init(const struct ubifs_info *c,
302  union ubifs_key *key, ino_t inum)
303 {
304  key->u32[0] = inum;
306 }
307 
315 static inline void invalid_key_init(const struct ubifs_info *c,
316  union ubifs_key *key)
317 {
318  key->u32[0] = 0xDEADBEAF;
319  key->u32[1] = UBIFS_INVALID_KEY;
320 }
321 
327 static inline int key_type(const struct ubifs_info *c,
328  const union ubifs_key *key)
329 {
330  return key->u32[1] >> UBIFS_S_KEY_BLOCK_BITS;
331 }
332 
338 static inline int key_type_flash(const struct ubifs_info *c, const void *k)
339 {
340  const union ubifs_key *key = k;
341 
342  return le32_to_cpu(key->j32[1]) >> UBIFS_S_KEY_BLOCK_BITS;
343 }
344 
350 static inline ino_t key_inum(const struct ubifs_info *c, const void *k)
351 {
352  const union ubifs_key *key = k;
353 
354  return key->u32[0];
355 }
356 
362 static inline ino_t key_inum_flash(const struct ubifs_info *c, const void *k)
363 {
364  const union ubifs_key *key = k;
365 
366  return le32_to_cpu(key->j32[0]);
367 }
368 
374 static inline uint32_t key_hash(const struct ubifs_info *c,
375  const union ubifs_key *key)
376 {
377  return key->u32[1] & UBIFS_S_KEY_HASH_MASK;
378 }
379 
385 static inline uint32_t key_hash_flash(const struct ubifs_info *c, const void *k)
386 {
387  const union ubifs_key *key = k;
388 
389  return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_HASH_MASK;
390 }
391 
397 static inline unsigned int key_block(const struct ubifs_info *c,
398  const union ubifs_key *key)
399 {
400  return key->u32[1] & UBIFS_S_KEY_BLOCK_MASK;
401 }
402 
408 static inline unsigned int key_block_flash(const struct ubifs_info *c,
409  const void *k)
410 {
411  const union ubifs_key *key = k;
412 
413  return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_BLOCK_MASK;
414 }
415 
422 static inline void key_read(const struct ubifs_info *c, const void *from,
423  union ubifs_key *to)
424 {
425  const union ubifs_key *f = from;
426 
427  to->u32[0] = le32_to_cpu(f->j32[0]);
428  to->u32[1] = le32_to_cpu(f->j32[1]);
429 }
430 
437 static inline void key_write(const struct ubifs_info *c,
438  const union ubifs_key *from, void *to)
439 {
440  union ubifs_key *t = to;
441 
442  t->j32[0] = cpu_to_le32(from->u32[0]);
443  t->j32[1] = cpu_to_le32(from->u32[1]);
444  memset(to + 8, 0, UBIFS_MAX_KEY_LEN - 8);
445 }
446 
453 static inline void key_write_idx(const struct ubifs_info *c,
454  const union ubifs_key *from, void *to)
455 {
456  union ubifs_key *t = to;
457 
458  t->j32[0] = cpu_to_le32(from->u32[0]);
459  t->j32[1] = cpu_to_le32(from->u32[1]);
460 }
461 
468 static inline void key_copy(const struct ubifs_info *c,
469  const union ubifs_key *from, union ubifs_key *to)
470 {
471  to->u64[0] = from->u64[0];
472 }
473 
483 static inline int keys_cmp(const struct ubifs_info *c,
484  const union ubifs_key *key1,
485  const union ubifs_key *key2)
486 {
487  if (key1->u32[0] < key2->u32[0])
488  return -1;
489  if (key1->u32[0] > key2->u32[0])
490  return 1;
491  if (key1->u32[1] < key2->u32[1])
492  return -1;
493  if (key1->u32[1] > key2->u32[1])
494  return 1;
495 
496  return 0;
497 }
498 
508 static inline int keys_eq(const struct ubifs_info *c,
509  const union ubifs_key *key1,
510  const union ubifs_key *key2)
511 {
512  if (key1->u32[0] != key2->u32[0])
513  return 0;
514  if (key1->u32[1] != key2->u32[1])
515  return 0;
516  return 1;
517 }
518 
526 static inline int is_hash_key(const struct ubifs_info *c,
527  const union ubifs_key *key)
528 {
529  int type = key_type(c, key);
530 
531  return type == UBIFS_DENT_KEY || type == UBIFS_XENT_KEY;
532 }
533 
538 static inline unsigned long long key_max_inode_size(const struct ubifs_info *c)
539 {
540  switch (c->key_fmt) {
542  return (1ULL << UBIFS_S_KEY_BLOCK_BITS) * UBIFS_BLOCK_SIZE;
543  default:
544  return 0;
545  }
546 }
547 
548 #endif /* !__UBIFS_KEY_H__ */