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
26
27
28
29
30
31
32
33
34
35
36
37#ifndef __UBIFS_KEY_H__
38#define __UBIFS_KEY_H__
39
40
41
42
43
44
45
46
47
48static 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
56
57
58
59
60
61static 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
76
77
78
79
80
81static 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
90
91
92
93
94
95
96static inline void ino_key_init(const struct ubifs_info *c,
97 union ubifs_key *key, ino_t inum)
98{
99 key->u32[0] = inum;
100 key->u32[1] = UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS;
101}
102
103
104
105
106
107
108
109static 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);
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);
117}
118
119
120
121
122
123
124
125static 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
132
133
134
135
136
137
138static 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
145
146
147
148
149
150
151
152static 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
158 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
159 key->u32[0] = inum;
160 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
161}
162
163
164
165
166
167
168
169
170
171static inline void dent_key_init_hash(const struct ubifs_info *c,
172 union ubifs_key *key, ino_t inum,
173 uint32_t hash)
174{
175 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
176 key->u32[0] = inum;
177 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
178}
179
180
181
182
183
184
185
186
187static 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
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);
198}
199
200
201
202
203
204
205
206static inline void lowest_dent_key(const struct ubifs_info *c,
207 union ubifs_key *key, ino_t inum)
208{
209 key->u32[0] = inum;
210 key->u32[1] = UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS;
211}
212
213
214
215
216
217
218
219
220static 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
226 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
227 key->u32[0] = inum;
228 key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
229}
230
231
232
233
234
235
236
237
238
239static inline void xent_key_init_hash(const struct ubifs_info *c,
240 union ubifs_key *key, ino_t inum,
241 uint32_t hash)
242{
243 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
244 key->u32[0] = inum;
245 key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
246}
247
248
249
250
251
252
253
254
255static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
256 ino_t inum, const struct qstr *nm)
257{
258 union ubifs_key *key = k;
259 uint32_t hash = c->key_hash(nm->name, nm->len);
260
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);
266}
267
268
269
270
271
272
273
274static inline void lowest_xent_key(const struct ubifs_info *c,
275 union ubifs_key *key, ino_t inum)
276{
277 key->u32[0] = inum;
278 key->u32[1] = UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS;
279}
280
281
282
283
284
285
286
287
288static inline void data_key_init(const struct ubifs_info *c,
289 union ubifs_key *key, ino_t inum,
290 unsigned int block)
291{
292 ubifs_assert(!(block & ~UBIFS_S_KEY_BLOCK_MASK));
293 key->u32[0] = inum;
294 key->u32[1] = block | (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS);
295}
296
297
298
299
300
301
302
303
304static inline void data_key_init_flash(const struct ubifs_info *c, void *k,
305 ino_t inum, unsigned int block)
306{
307 union ubifs_key *key = k;
308
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);
314}
315
316
317
318
319
320
321
322
323
324
325static inline void trun_key_init(const struct ubifs_info *c,
326 union ubifs_key *key, ino_t inum)
327{
328 key->u32[0] = inum;
329 key->u32[1] = UBIFS_TRUN_KEY << UBIFS_S_KEY_BLOCK_BITS;
330}
331
332
333
334
335
336
337static inline int key_type(const struct ubifs_info *c,
338 const union ubifs_key *key)
339{
340 return key->u32[1] >> UBIFS_S_KEY_BLOCK_BITS;
341}
342
343
344
345
346
347
348static inline int key_type_flash(const struct ubifs_info *c, const void *k)
349{
350 const union ubifs_key *key = k;
351
352 return le32_to_cpu(key->j32[1]) >> UBIFS_S_KEY_BLOCK_BITS;
353}
354
355
356
357
358
359
360static inline ino_t key_inum(const struct ubifs_info *c, const void *k)
361{
362 const union ubifs_key *key = k;
363
364 return key->u32[0];
365}
366
367
368
369
370
371
372static inline ino_t key_inum_flash(const struct ubifs_info *c, const void *k)
373{
374 const union ubifs_key *key = k;
375
376 return le32_to_cpu(key->j32[0]);
377}
378
379
380
381
382
383
384static inline int key_hash(const struct ubifs_info *c,
385 const union ubifs_key *key)
386{
387 return key->u32[1] & UBIFS_S_KEY_HASH_MASK;
388}
389
390
391
392
393
394
395static inline int key_hash_flash(const struct ubifs_info *c, const void *k)
396{
397 const union ubifs_key *key = k;
398
399 return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_HASH_MASK;
400}
401
402
403
404
405
406
407static inline unsigned int key_block(const struct ubifs_info *c,
408 const union ubifs_key *key)
409{
410 return key->u32[1] & UBIFS_S_KEY_BLOCK_MASK;
411}
412
413
414
415
416
417
418static inline unsigned int key_block_flash(const struct ubifs_info *c,
419 const void *k)
420{
421 const union ubifs_key *key = k;
422
423 return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_BLOCK_MASK;
424}
425
426
427
428
429
430
431
432static inline void key_read(const struct ubifs_info *c, const void *from,
433 union ubifs_key *to)
434{
435 const union ubifs_key *f = from;
436
437 to->u32[0] = le32_to_cpu(f->j32[0]);
438 to->u32[1] = le32_to_cpu(f->j32[1]);
439}
440
441
442
443
444
445
446
447static inline void key_write(const struct ubifs_info *c,
448 const union ubifs_key *from, void *to)
449{
450 union ubifs_key *t = to;
451
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);
455}
456
457
458
459
460
461
462
463static inline void key_write_idx(const struct ubifs_info *c,
464 const union ubifs_key *from, void *to)
465{
466 union ubifs_key *t = to;
467
468 t->j32[0] = cpu_to_le32(from->u32[0]);
469 t->j32[1] = cpu_to_le32(from->u32[1]);
470}
471
472
473
474
475
476
477
478static inline void key_copy(const struct ubifs_info *c,
479 const union ubifs_key *from, union ubifs_key *to)
480{
481 to->u64[0] = from->u64[0];
482}
483
484
485
486
487
488
489
490
491
492
493static inline int keys_cmp(const struct ubifs_info *c,
494 const union ubifs_key *key1,
495 const union ubifs_key *key2)
496{
497 if (key1->u32[0] < key2->u32[0])
498 return -1;
499 if (key1->u32[0] > key2->u32[0])
500 return 1;
501 if (key1->u32[1] < key2->u32[1])
502 return -1;
503 if (key1->u32[1] > key2->u32[1])
504 return 1;
505
506 return 0;
507}
508
509
510
511
512
513
514
515
516
517
518static inline int keys_eq(const struct ubifs_info *c,
519 const union ubifs_key *key1,
520 const union ubifs_key *key2)
521{
522 if (key1->u32[0] != key2->u32[0])
523 return 0;
524 if (key1->u32[1] != key2->u32[1])
525 return 0;
526 return 1;
527}
528
529
530
531
532
533
534
535
536static inline int is_hash_key(const struct ubifs_info *c,
537 const union ubifs_key *key)
538{
539 int type = key_type(c, key);
540
541 return type == UBIFS_DENT_KEY || type == UBIFS_XENT_KEY;
542}
543
544
545
546
547
548static inline unsigned long long key_max_inode_size(const struct ubifs_info *c)
549{
550 switch (c->key_fmt) {
551 case UBIFS_SIMPLE_KEY_FMT:
552 return (1ULL << UBIFS_S_KEY_BLOCK_BITS) * UBIFS_BLOCK_SIZE;
553 default:
554 return 0;
555 }
556}
557#endif
558