1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#ifndef __UBIFS_KEY_H__
26#define __UBIFS_KEY_H__
27
28
29
30
31
32
33
34
35
36static inline uint32_t key_mask_hash(uint32_t hash)
37{
38 hash &= UBIFS_S_KEY_HASH_MASK;
39 if (unlikely(hash <= 2))
40 hash += 3;
41 return hash;
42}
43
44
45
46
47
48
49static inline uint32_t key_r5_hash(const char *s, int len)
50{
51 uint32_t a = 0;
52 const signed char *str = (const signed char *)s;
53
54 while (*str) {
55 a += *str << 4;
56 a += *str >> 4;
57 a *= 11;
58 str++;
59 }
60
61 return key_mask_hash(a);
62}
63
64
65
66
67
68
69static inline uint32_t key_test_hash(const char *str, int len)
70{
71 uint32_t a = 0;
72
73 len = min_t(uint32_t, len, 4);
74 memcpy(&a, str, len);
75 return key_mask_hash(a);
76}
77
78
79
80
81
82
83
84static inline void ino_key_init(const struct ubifs_info *c,
85 union ubifs_key *key, ino_t inum)
86{
87 key->u32[0] = inum;
88 key->u32[1] = UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS;
89}
90
91
92
93
94
95
96
97static inline void ino_key_init_flash(const struct ubifs_info *c, void *k,
98 ino_t inum)
99{
100 union ubifs_key *key = k;
101
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);
105}
106
107
108
109
110
111
112
113static inline void lowest_ino_key(const struct ubifs_info *c,
114 union ubifs_key *key, ino_t inum)
115{
116 key->u32[0] = inum;
117 key->u32[1] = 0;
118}
119
120
121
122
123
124
125
126static inline void highest_ino_key(const struct ubifs_info *c,
127 union ubifs_key *key, ino_t inum)
128{
129 key->u32[0] = inum;
130 key->u32[1] = 0xffffffff;
131}
132
133
134
135
136
137
138
139
140static inline void dent_key_init(const struct ubifs_info *c,
141 union ubifs_key *key, ino_t inum,
142 const struct qstr *nm)
143{
144 uint32_t hash = c->key_hash(nm->name, nm->len);
145
146 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
147 key->u32[0] = inum;
148 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
149}
150
151
152
153
154
155
156
157
158
159static inline void dent_key_init_hash(const struct ubifs_info *c,
160 union ubifs_key *key, ino_t inum,
161 uint32_t hash)
162{
163 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
164 key->u32[0] = inum;
165 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
166}
167
168
169
170
171
172
173
174
175static inline void dent_key_init_flash(const struct ubifs_info *c, void *k,
176 ino_t inum, const struct qstr *nm)
177{
178 union ubifs_key *key = k;
179 uint32_t hash = c->key_hash(nm->name, nm->len);
180
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);
186}
187
188
189
190
191
192
193
194static inline void lowest_dent_key(const struct ubifs_info *c,
195 union ubifs_key *key, ino_t inum)
196{
197 key->u32[0] = inum;
198 key->u32[1] = UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS;
199}
200
201
202
203
204
205
206
207
208static inline void xent_key_init(const struct ubifs_info *c,
209 union ubifs_key *key, ino_t inum,
210 const struct qstr *nm)
211{
212 uint32_t hash = c->key_hash(nm->name, nm->len);
213
214 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
215 key->u32[0] = inum;
216 key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
217}
218
219
220
221
222
223
224
225
226static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
227 ino_t inum, const struct qstr *nm)
228{
229 union ubifs_key *key = k;
230 uint32_t hash = c->key_hash(nm->name, nm->len);
231
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);
237}
238
239
240
241
242
243
244
245static inline void lowest_xent_key(const struct ubifs_info *c,
246 union ubifs_key *key, ino_t inum)
247{
248 key->u32[0] = inum;
249 key->u32[1] = UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS;
250}
251
252
253
254
255
256
257
258
259static inline void data_key_init(const struct ubifs_info *c,
260 union ubifs_key *key, ino_t inum,
261 unsigned int block)
262{
263 ubifs_assert(!(block & ~UBIFS_S_KEY_BLOCK_MASK));
264 key->u32[0] = inum;
265 key->u32[1] = block | (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS);
266}
267
268
269
270
271
272
273
274static inline void highest_data_key(const struct ubifs_info *c,
275 union ubifs_key *key, ino_t inum)
276{
277 data_key_init(c, key, inum, UBIFS_S_KEY_BLOCK_MASK);
278}
279
280
281
282
283
284
285
286
287
288
289static inline void trun_key_init(const struct ubifs_info *c,
290 union ubifs_key *key, ino_t inum)
291{
292 key->u32[0] = inum;
293 key->u32[1] = UBIFS_TRUN_KEY << UBIFS_S_KEY_BLOCK_BITS;
294}
295
296
297
298
299
300
301
302
303static inline void invalid_key_init(const struct ubifs_info *c,
304 union ubifs_key *key)
305{
306 key->u32[0] = 0xDEADBEAF;
307 key->u32[1] = UBIFS_INVALID_KEY;
308}
309
310
311
312
313
314
315static inline int key_type(const struct ubifs_info *c,
316 const union ubifs_key *key)
317{
318 return key->u32[1] >> UBIFS_S_KEY_BLOCK_BITS;
319}
320
321
322
323
324
325
326static inline int key_type_flash(const struct ubifs_info *c, const void *k)
327{
328 const union ubifs_key *key = k;
329
330 return le32_to_cpu(key->j32[1]) >> UBIFS_S_KEY_BLOCK_BITS;
331}
332
333
334
335
336
337
338static inline ino_t key_inum(const struct ubifs_info *c, const void *k)
339{
340 const union ubifs_key *key = k;
341
342 return key->u32[0];
343}
344
345
346
347
348
349
350static inline ino_t key_inum_flash(const struct ubifs_info *c, const void *k)
351{
352 const union ubifs_key *key = k;
353
354 return le32_to_cpu(key->j32[0]);
355}
356
357
358
359
360
361
362static inline uint32_t key_hash(const struct ubifs_info *c,
363 const union ubifs_key *key)
364{
365 return key->u32[1] & UBIFS_S_KEY_HASH_MASK;
366}
367
368
369
370
371
372
373static inline uint32_t key_hash_flash(const struct ubifs_info *c, const void *k)
374{
375 const union ubifs_key *key = k;
376
377 return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_HASH_MASK;
378}
379
380
381
382
383
384
385static inline unsigned int key_block(const struct ubifs_info *c,
386 const union ubifs_key *key)
387{
388 return key->u32[1] & UBIFS_S_KEY_BLOCK_MASK;
389}
390
391
392
393
394
395
396static inline unsigned int key_block_flash(const struct ubifs_info *c,
397 const void *k)
398{
399 const union ubifs_key *key = k;
400
401 return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_BLOCK_MASK;
402}
403
404
405
406
407
408
409
410static inline void key_read(const struct ubifs_info *c, const void *from,
411 union ubifs_key *to)
412{
413 const union ubifs_key *f = from;
414
415 to->u32[0] = le32_to_cpu(f->j32[0]);
416 to->u32[1] = le32_to_cpu(f->j32[1]);
417}
418
419
420
421
422
423
424
425static inline void key_write(const struct ubifs_info *c,
426 const union ubifs_key *from, void *to)
427{
428 union ubifs_key *t = to;
429
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);
433}
434
435
436
437
438
439
440
441static inline void key_write_idx(const struct ubifs_info *c,
442 const union ubifs_key *from, void *to)
443{
444 union ubifs_key *t = to;
445
446 t->j32[0] = cpu_to_le32(from->u32[0]);
447 t->j32[1] = cpu_to_le32(from->u32[1]);
448}
449
450
451
452
453
454
455
456static inline void key_copy(const struct ubifs_info *c,
457 const union ubifs_key *from, union ubifs_key *to)
458{
459 to->u64[0] = from->u64[0];
460}
461
462
463
464
465
466
467
468
469
470
471static inline int keys_cmp(const struct ubifs_info *c,
472 const union ubifs_key *key1,
473 const union ubifs_key *key2)
474{
475 if (key1->u32[0] < key2->u32[0])
476 return -1;
477 if (key1->u32[0] > key2->u32[0])
478 return 1;
479 if (key1->u32[1] < key2->u32[1])
480 return -1;
481 if (key1->u32[1] > key2->u32[1])
482 return 1;
483
484 return 0;
485}
486
487
488
489
490
491
492
493
494
495
496static inline int keys_eq(const struct ubifs_info *c,
497 const union ubifs_key *key1,
498 const union ubifs_key *key2)
499{
500 if (key1->u32[0] != key2->u32[0])
501 return 0;
502 if (key1->u32[1] != key2->u32[1])
503 return 0;
504 return 1;
505}
506
507
508
509
510
511
512
513
514static inline int is_hash_key(const struct ubifs_info *c,
515 const union ubifs_key *key)
516{
517 int type = key_type(c, key);
518
519 return type == UBIFS_DENT_KEY || type == UBIFS_XENT_KEY;
520}
521
522
523
524
525
526static inline unsigned long long key_max_inode_size(const struct ubifs_info *c)
527{
528 switch (c->key_fmt) {
529 case UBIFS_SIMPLE_KEY_FMT:
530 return (1ULL << UBIFS_S_KEY_BLOCK_BITS) * UBIFS_BLOCK_SIZE;
531 default:
532 return 0;
533 }
534}
535
536#endif
537