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