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