0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #ifndef __UBIFS_KEY_H__
0032 #define __UBIFS_KEY_H__
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 static inline uint32_t key_mask_hash(uint32_t hash)
0043 {
0044 hash &= UBIFS_S_KEY_HASH_MASK;
0045 if (unlikely(hash <= 2))
0046 hash += 3;
0047 return hash;
0048 }
0049
0050
0051
0052
0053
0054
0055 static inline uint32_t key_r5_hash(const char *s, int len)
0056 {
0057 uint32_t a = 0;
0058 const signed char *str = (const signed char *)s;
0059
0060 while (len--) {
0061 a += *str << 4;
0062 a += *str >> 4;
0063 a *= 11;
0064 str++;
0065 }
0066
0067 return key_mask_hash(a);
0068 }
0069
0070
0071
0072
0073
0074
0075 static inline uint32_t key_test_hash(const char *str, int len)
0076 {
0077 uint32_t a = 0;
0078
0079 len = min_t(uint32_t, len, 4);
0080 memcpy(&a, str, len);
0081 return key_mask_hash(a);
0082 }
0083
0084
0085
0086
0087
0088
0089
0090 static inline void ino_key_init(const struct ubifs_info *c,
0091 union ubifs_key *key, ino_t inum)
0092 {
0093 key->u32[0] = inum;
0094 key->u32[1] = UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS;
0095 }
0096
0097
0098
0099
0100
0101
0102
0103 static inline void ino_key_init_flash(const struct ubifs_info *c, void *k,
0104 ino_t inum)
0105 {
0106 union ubifs_key *key = k;
0107
0108 key->j32[0] = cpu_to_le32(inum);
0109 key->j32[1] = cpu_to_le32(UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS);
0110 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
0111 }
0112
0113
0114
0115
0116
0117
0118
0119 static inline void lowest_ino_key(const struct ubifs_info *c,
0120 union ubifs_key *key, ino_t inum)
0121 {
0122 key->u32[0] = inum;
0123 key->u32[1] = 0;
0124 }
0125
0126
0127
0128
0129
0130
0131
0132 static inline void highest_ino_key(const struct ubifs_info *c,
0133 union ubifs_key *key, ino_t inum)
0134 {
0135 key->u32[0] = inum;
0136 key->u32[1] = 0xffffffff;
0137 }
0138
0139
0140
0141
0142
0143
0144
0145
0146 static inline void dent_key_init(const struct ubifs_info *c,
0147 union ubifs_key *key, ino_t inum,
0148 const struct fscrypt_name *nm)
0149 {
0150 uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
0151
0152 ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
0153 key->u32[0] = inum;
0154 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
0155 }
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165 static inline void dent_key_init_hash(const struct ubifs_info *c,
0166 union ubifs_key *key, ino_t inum,
0167 uint32_t hash)
0168 {
0169 ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
0170 key->u32[0] = inum;
0171 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
0172 }
0173
0174
0175
0176
0177
0178
0179
0180
0181 static inline void dent_key_init_flash(const struct ubifs_info *c, void *k,
0182 ino_t inum,
0183 const struct fscrypt_name *nm)
0184 {
0185 union ubifs_key *key = k;
0186 uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
0187
0188 ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
0189 key->j32[0] = cpu_to_le32(inum);
0190 key->j32[1] = cpu_to_le32(hash |
0191 (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS));
0192 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
0193 }
0194
0195
0196
0197
0198
0199
0200
0201 static inline void lowest_dent_key(const struct ubifs_info *c,
0202 union ubifs_key *key, ino_t inum)
0203 {
0204 key->u32[0] = inum;
0205 key->u32[1] = UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS;
0206 }
0207
0208
0209
0210
0211
0212
0213
0214
0215 static inline void xent_key_init(const struct ubifs_info *c,
0216 union ubifs_key *key, ino_t inum,
0217 const struct fscrypt_name *nm)
0218 {
0219 uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
0220
0221 ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
0222 key->u32[0] = inum;
0223 key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
0224 }
0225
0226
0227
0228
0229
0230
0231
0232
0233 static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
0234 ino_t inum, const struct fscrypt_name *nm)
0235 {
0236 union ubifs_key *key = k;
0237 uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
0238
0239 ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
0240 key->j32[0] = cpu_to_le32(inum);
0241 key->j32[1] = cpu_to_le32(hash |
0242 (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS));
0243 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
0244 }
0245
0246
0247
0248
0249
0250
0251
0252 static inline void lowest_xent_key(const struct ubifs_info *c,
0253 union ubifs_key *key, ino_t inum)
0254 {
0255 key->u32[0] = inum;
0256 key->u32[1] = UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS;
0257 }
0258
0259
0260
0261
0262
0263
0264
0265
0266 static inline void data_key_init(const struct ubifs_info *c,
0267 union ubifs_key *key, ino_t inum,
0268 unsigned int block)
0269 {
0270 ubifs_assert(c, !(block & ~UBIFS_S_KEY_BLOCK_MASK));
0271 key->u32[0] = inum;
0272 key->u32[1] = block | (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS);
0273 }
0274
0275
0276
0277
0278
0279
0280
0281 static inline void highest_data_key(const struct ubifs_info *c,
0282 union ubifs_key *key, ino_t inum)
0283 {
0284 data_key_init(c, key, inum, UBIFS_S_KEY_BLOCK_MASK);
0285 }
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296 static inline void trun_key_init(const struct ubifs_info *c,
0297 union ubifs_key *key, ino_t inum)
0298 {
0299 key->u32[0] = inum;
0300 key->u32[1] = UBIFS_TRUN_KEY << UBIFS_S_KEY_BLOCK_BITS;
0301 }
0302
0303
0304
0305
0306
0307
0308
0309
0310 static inline void invalid_key_init(const struct ubifs_info *c,
0311 union ubifs_key *key)
0312 {
0313 key->u32[0] = 0xDEADBEAF;
0314 key->u32[1] = UBIFS_INVALID_KEY;
0315 }
0316
0317
0318
0319
0320
0321
0322 static inline int key_type(const struct ubifs_info *c,
0323 const union ubifs_key *key)
0324 {
0325 return key->u32[1] >> UBIFS_S_KEY_BLOCK_BITS;
0326 }
0327
0328
0329
0330
0331
0332
0333 static inline int key_type_flash(const struct ubifs_info *c, const void *k)
0334 {
0335 const union ubifs_key *key = k;
0336
0337 return le32_to_cpu(key->j32[1]) >> UBIFS_S_KEY_BLOCK_BITS;
0338 }
0339
0340
0341
0342
0343
0344
0345 static inline ino_t key_inum(const struct ubifs_info *c, const void *k)
0346 {
0347 const union ubifs_key *key = k;
0348
0349 return key->u32[0];
0350 }
0351
0352
0353
0354
0355
0356
0357 static inline ino_t key_inum_flash(const struct ubifs_info *c, const void *k)
0358 {
0359 const union ubifs_key *key = k;
0360
0361 return le32_to_cpu(key->j32[0]);
0362 }
0363
0364
0365
0366
0367
0368
0369 static inline uint32_t key_hash(const struct ubifs_info *c,
0370 const union ubifs_key *key)
0371 {
0372 return key->u32[1] & UBIFS_S_KEY_HASH_MASK;
0373 }
0374
0375
0376
0377
0378
0379
0380 static inline uint32_t key_hash_flash(const struct ubifs_info *c, const void *k)
0381 {
0382 const union ubifs_key *key = k;
0383
0384 return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_HASH_MASK;
0385 }
0386
0387
0388
0389
0390
0391
0392 static inline unsigned int key_block(const struct ubifs_info *c,
0393 const union ubifs_key *key)
0394 {
0395 return key->u32[1] & UBIFS_S_KEY_BLOCK_MASK;
0396 }
0397
0398
0399
0400
0401
0402
0403 static inline unsigned int key_block_flash(const struct ubifs_info *c,
0404 const void *k)
0405 {
0406 const union ubifs_key *key = k;
0407
0408 return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_BLOCK_MASK;
0409 }
0410
0411
0412
0413
0414
0415
0416
0417 static inline void key_read(const struct ubifs_info *c, const void *from,
0418 union ubifs_key *to)
0419 {
0420 const union ubifs_key *f = from;
0421
0422 to->u32[0] = le32_to_cpu(f->j32[0]);
0423 to->u32[1] = le32_to_cpu(f->j32[1]);
0424 }
0425
0426
0427
0428
0429
0430
0431
0432 static inline void key_write(const struct ubifs_info *c,
0433 const union ubifs_key *from, void *to)
0434 {
0435 union ubifs_key *t = to;
0436
0437 t->j32[0] = cpu_to_le32(from->u32[0]);
0438 t->j32[1] = cpu_to_le32(from->u32[1]);
0439 memset(to + 8, 0, UBIFS_MAX_KEY_LEN - 8);
0440 }
0441
0442
0443
0444
0445
0446
0447
0448 static inline void key_write_idx(const struct ubifs_info *c,
0449 const union ubifs_key *from, void *to)
0450 {
0451 union ubifs_key *t = to;
0452
0453 t->j32[0] = cpu_to_le32(from->u32[0]);
0454 t->j32[1] = cpu_to_le32(from->u32[1]);
0455 }
0456
0457
0458
0459
0460
0461
0462
0463 static inline void key_copy(const struct ubifs_info *c,
0464 const union ubifs_key *from, union ubifs_key *to)
0465 {
0466 to->u64[0] = from->u64[0];
0467 }
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478 static inline int keys_cmp(const struct ubifs_info *c,
0479 const union ubifs_key *key1,
0480 const union ubifs_key *key2)
0481 {
0482 if (key1->u32[0] < key2->u32[0])
0483 return -1;
0484 if (key1->u32[0] > key2->u32[0])
0485 return 1;
0486 if (key1->u32[1] < key2->u32[1])
0487 return -1;
0488 if (key1->u32[1] > key2->u32[1])
0489 return 1;
0490
0491 return 0;
0492 }
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503 static inline int keys_eq(const struct ubifs_info *c,
0504 const union ubifs_key *key1,
0505 const union ubifs_key *key2)
0506 {
0507 if (key1->u32[0] != key2->u32[0])
0508 return 0;
0509 if (key1->u32[1] != key2->u32[1])
0510 return 0;
0511 return 1;
0512 }
0513
0514
0515
0516
0517
0518
0519
0520
0521 static inline int is_hash_key(const struct ubifs_info *c,
0522 const union ubifs_key *key)
0523 {
0524 int type = key_type(c, key);
0525
0526 return type == UBIFS_DENT_KEY || type == UBIFS_XENT_KEY;
0527 }
0528
0529
0530
0531
0532
0533 static inline unsigned long long key_max_inode_size(const struct ubifs_info *c)
0534 {
0535 switch (c->key_fmt) {
0536 case UBIFS_SIMPLE_KEY_FMT:
0537 return (1ULL << UBIFS_S_KEY_BLOCK_BITS) * UBIFS_BLOCK_SIZE;
0538 default:
0539 return 0;
0540 }
0541 }
0542
0543 #endif