key.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. *
  7. * Authors: Artem Bityutskiy (Битюцкий Артём)
  8. * Adrian Hunter
  9. */
  10. /*
  11. * This header contains various key-related definitions and helper function.
  12. * UBIFS allows several key schemes, so we access key fields only via these
  13. * helpers. At the moment only one key scheme is supported.
  14. *
  15. * Simple key scheme
  16. * ~~~~~~~~~~~~~~~~~
  17. *
  18. * Keys are 64-bits long. First 32-bits are inode number (parent inode number
  19. * in case of direntry key). Next 3 bits are node type. The last 29 bits are
  20. * 4KiB offset in case of inode node, and direntry hash in case of a direntry
  21. * node. We use "r5" hash borrowed from reiserfs.
  22. */
  23. /*
  24. * Lot's of the key helpers require a struct ubifs_info *c as the first parameter.
  25. * But we are not using it at all currently. That's designed for future extensions of
  26. * different c->key_format. But right now, there is only one key type, UBIFS_SIMPLE_KEY_FMT.
  27. */
  28. #ifndef __UBIFS_KEY_H__
  29. #define __UBIFS_KEY_H__
  30. /**
  31. * key_mask_hash - mask a valid hash value.
  32. * @val: value to be masked
  33. *
  34. * We use hash values as offset in directories, so values %0 and %1 are
  35. * reserved for "." and "..". %2 is reserved for "end of readdir" marker. This
  36. * function makes sure the reserved values are not used.
  37. */
  38. static inline uint32_t key_mask_hash(uint32_t hash)
  39. {
  40. hash &= UBIFS_S_KEY_HASH_MASK;
  41. if (unlikely(hash <= 2))
  42. hash += 3;
  43. return hash;
  44. }
  45. /**
  46. * key_r5_hash - R5 hash function (borrowed from reiserfs).
  47. * @s: direntry name
  48. * @len: name length
  49. */
  50. static inline uint32_t key_r5_hash(const char *s, int len)
  51. {
  52. uint32_t a = 0;
  53. const signed char *str = (const signed char *)s;
  54. while (len--) {
  55. a += *str << 4;
  56. a += *str >> 4;
  57. a *= 11;
  58. str++;
  59. }
  60. return key_mask_hash(a);
  61. }
  62. /**
  63. * key_test_hash - testing hash function.
  64. * @str: direntry name
  65. * @len: name length
  66. */
  67. static inline uint32_t key_test_hash(const char *str, int len)
  68. {
  69. uint32_t a = 0;
  70. len = min_t(uint32_t, len, 4);
  71. memcpy(&a, str, len);
  72. return key_mask_hash(a);
  73. }
  74. /**
  75. * ino_key_init - initialize inode key.
  76. * @c: UBIFS file-system description object
  77. * @key: key to initialize
  78. * @inum: inode number
  79. */
  80. static inline void ino_key_init(const struct ubifs_info *c,
  81. union ubifs_key *key, ino_t inum)
  82. {
  83. key->u32[0] = inum;
  84. key->u32[1] = UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS;
  85. }
  86. /**
  87. * ino_key_init_flash - initialize on-flash inode key.
  88. * @c: UBIFS file-system description object
  89. * @k: key to initialize
  90. * @inum: inode number
  91. */
  92. static inline void ino_key_init_flash(const struct ubifs_info *c, void *k,
  93. ino_t inum)
  94. {
  95. union ubifs_key *key = k;
  96. key->j32[0] = cpu_to_le32(inum);
  97. key->j32[1] = cpu_to_le32(UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS);
  98. memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
  99. }
  100. /**
  101. * lowest_ino_key - get the lowest possible inode key.
  102. * @c: UBIFS file-system description object
  103. * @key: key to initialize
  104. * @inum: inode number
  105. */
  106. static inline void lowest_ino_key(const struct ubifs_info *c,
  107. union ubifs_key *key, ino_t inum)
  108. {
  109. key->u32[0] = inum;
  110. key->u32[1] = 0;
  111. }
  112. /**
  113. * highest_ino_key - get the highest possible inode key.
  114. * @c: UBIFS file-system description object
  115. * @key: key to initialize
  116. * @inum: inode number
  117. */
  118. static inline void highest_ino_key(const struct ubifs_info *c,
  119. union ubifs_key *key, ino_t inum)
  120. {
  121. key->u32[0] = inum;
  122. key->u32[1] = 0xffffffff;
  123. }
  124. /**
  125. * dent_key_init - initialize directory entry key.
  126. * @c: UBIFS file-system description object
  127. * @key: key to initialize
  128. * @inum: parent inode number
  129. * @nm: direntry name and length. Not a string when encrypted!
  130. */
  131. static inline void dent_key_init(const struct ubifs_info *c,
  132. union ubifs_key *key, ino_t inum,
  133. const struct fscrypt_name *nm)
  134. {
  135. uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
  136. ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
  137. key->u32[0] = inum;
  138. key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
  139. }
  140. /**
  141. * dent_key_init_hash - initialize directory entry key without re-calculating
  142. * hash function.
  143. * @c: UBIFS file-system description object
  144. * @key: key to initialize
  145. * @inum: parent inode number
  146. * @hash: direntry name hash
  147. */
  148. static inline void dent_key_init_hash(const struct ubifs_info *c,
  149. union ubifs_key *key, ino_t inum,
  150. uint32_t hash)
  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. * dent_key_init_flash - initialize on-flash directory entry key.
  158. * @c: UBIFS file-system description object
  159. * @k: key to initialize
  160. * @inum: parent inode number
  161. * @nm: direntry name and length
  162. */
  163. static inline void dent_key_init_flash(const struct ubifs_info *c, void *k,
  164. ino_t inum,
  165. const struct fscrypt_name *nm)
  166. {
  167. union ubifs_key *key = k;
  168. uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
  169. ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
  170. key->j32[0] = cpu_to_le32(inum);
  171. key->j32[1] = cpu_to_le32(hash |
  172. (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS));
  173. memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
  174. }
  175. /**
  176. * lowest_dent_key - get the lowest possible directory entry key.
  177. * @c: UBIFS file-system description object
  178. * @key: where to store the lowest key
  179. * @inum: parent inode number
  180. */
  181. static inline void lowest_dent_key(const struct ubifs_info *c,
  182. union ubifs_key *key, ino_t inum)
  183. {
  184. key->u32[0] = inum;
  185. key->u32[1] = UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS;
  186. }
  187. /**
  188. * xent_key_init - initialize extended attribute entry key.
  189. * @c: UBIFS file-system description object
  190. * @key: key to initialize
  191. * @inum: host inode number
  192. * @nm: extended attribute entry name and length
  193. */
  194. static inline void xent_key_init(const struct ubifs_info *c,
  195. union ubifs_key *key, ino_t inum,
  196. const struct fscrypt_name *nm)
  197. {
  198. uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
  199. ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
  200. key->u32[0] = inum;
  201. key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
  202. }
  203. /**
  204. * xent_key_init_flash - initialize on-flash extended attribute entry key.
  205. * @c: UBIFS file-system description object
  206. * @k: key to initialize
  207. * @inum: host inode number
  208. * @nm: extended attribute entry name and length
  209. */
  210. static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
  211. ino_t inum, const struct fscrypt_name *nm)
  212. {
  213. union ubifs_key *key = k;
  214. uint32_t hash = c->key_hash(fname_name(nm), fname_len(nm));
  215. ubifs_assert(c, !(hash & ~UBIFS_S_KEY_HASH_MASK));
  216. key->j32[0] = cpu_to_le32(inum);
  217. key->j32[1] = cpu_to_le32(hash |
  218. (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS));
  219. memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
  220. }
  221. /**
  222. * lowest_xent_key - get the lowest possible extended attribute entry key.
  223. * @c: UBIFS file-system description object
  224. * @key: where to store the lowest key
  225. * @inum: host inode number
  226. */
  227. static inline void lowest_xent_key(const struct ubifs_info *c,
  228. union ubifs_key *key, ino_t inum)
  229. {
  230. key->u32[0] = inum;
  231. key->u32[1] = UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS;
  232. }
  233. /**
  234. * data_key_init - initialize data key.
  235. * @c: UBIFS file-system description object
  236. * @key: key to initialize
  237. * @inum: inode number
  238. * @block: block number
  239. */
  240. static inline void data_key_init(const struct ubifs_info *c,
  241. union ubifs_key *key, ino_t inum,
  242. unsigned int block)
  243. {
  244. ubifs_assert(c, !(block & ~UBIFS_S_KEY_BLOCK_MASK));
  245. key->u32[0] = inum;
  246. key->u32[1] = block | (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS);
  247. }
  248. /**
  249. * highest_data_key - get the highest possible data key for an inode.
  250. * @c: UBIFS file-system description object
  251. * @key: key to initialize
  252. * @inum: inode number
  253. */
  254. static inline void highest_data_key(const struct ubifs_info *c,
  255. union ubifs_key *key, ino_t inum)
  256. {
  257. data_key_init(c, key, inum, UBIFS_S_KEY_BLOCK_MASK);
  258. }
  259. /**
  260. * trun_key_init - initialize truncation node key.
  261. * @c: UBIFS file-system description object
  262. * @key: key to initialize
  263. * @inum: inode number
  264. *
  265. * Note, UBIFS does not have truncation keys on the media and this function is
  266. * only used for purposes of replay.
  267. */
  268. static inline void trun_key_init(const struct ubifs_info *c,
  269. union ubifs_key *key, ino_t inum)
  270. {
  271. key->u32[0] = inum;
  272. key->u32[1] = UBIFS_TRUN_KEY << UBIFS_S_KEY_BLOCK_BITS;
  273. }
  274. /**
  275. * invalid_key_init - initialize invalid node key.
  276. * @c: UBIFS file-system description object
  277. * @key: key to initialize
  278. *
  279. * This is a helper function which marks a @key object as invalid.
  280. */
  281. static inline void invalid_key_init(const struct ubifs_info *c,
  282. union ubifs_key *key)
  283. {
  284. key->u32[0] = 0xDEADBEAF;
  285. key->u32[1] = UBIFS_INVALID_KEY;
  286. }
  287. /**
  288. * key_type - get key type.
  289. * @c: UBIFS file-system description object
  290. * @key: key to get type of
  291. */
  292. static inline int key_type(const struct ubifs_info *c,
  293. const union ubifs_key *key)
  294. {
  295. return key->u32[1] >> UBIFS_S_KEY_BLOCK_BITS;
  296. }
  297. /**
  298. * key_type_flash - get type of a on-flash formatted key.
  299. * @c: UBIFS file-system description object
  300. * @k: key to get type of
  301. */
  302. static inline int key_type_flash(const struct ubifs_info *c, const void *k)
  303. {
  304. const union ubifs_key *key = k;
  305. return le32_to_cpu(key->j32[1]) >> UBIFS_S_KEY_BLOCK_BITS;
  306. }
  307. /**
  308. * key_inum - fetch inode number from key.
  309. * @c: UBIFS file-system description object
  310. * @k: key to fetch inode number from
  311. */
  312. static inline ino_t key_inum(const struct ubifs_info *c, const void *k)
  313. {
  314. const union ubifs_key *key = k;
  315. return key->u32[0];
  316. }
  317. /**
  318. * key_inum_flash - fetch inode number from an on-flash formatted key.
  319. * @c: UBIFS file-system description object
  320. * @k: key to fetch inode number from
  321. */
  322. static inline ino_t key_inum_flash(const struct ubifs_info *c, const void *k)
  323. {
  324. const union ubifs_key *key = k;
  325. return le32_to_cpu(key->j32[0]);
  326. }
  327. /**
  328. * key_hash - get directory entry hash.
  329. * @c: UBIFS file-system description object
  330. * @key: the key to get hash from
  331. */
  332. static inline uint32_t key_hash(const struct ubifs_info *c,
  333. const union ubifs_key *key)
  334. {
  335. return key->u32[1] & UBIFS_S_KEY_HASH_MASK;
  336. }
  337. /**
  338. * key_hash_flash - get directory entry hash from an on-flash formatted key.
  339. * @c: UBIFS file-system description object
  340. * @k: the key to get hash from
  341. */
  342. static inline uint32_t key_hash_flash(const struct ubifs_info *c, const void *k)
  343. {
  344. const union ubifs_key *key = k;
  345. return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_HASH_MASK;
  346. }
  347. /**
  348. * key_block - get data block number.
  349. * @c: UBIFS file-system description object
  350. * @key: the key to get the block number from
  351. */
  352. static inline unsigned int key_block(const struct ubifs_info *c,
  353. const union ubifs_key *key)
  354. {
  355. return key->u32[1] & UBIFS_S_KEY_BLOCK_MASK;
  356. }
  357. /**
  358. * key_block_flash - get data block number from an on-flash formatted key.
  359. * @c: UBIFS file-system description object
  360. * @k: the key to get the block number from
  361. */
  362. static inline unsigned int key_block_flash(const struct ubifs_info *c,
  363. const void *k)
  364. {
  365. const union ubifs_key *key = k;
  366. return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_BLOCK_MASK;
  367. }
  368. /**
  369. * key_read - transform a key to in-memory format.
  370. * @c: UBIFS file-system description object
  371. * @from: the key to transform
  372. * @to: the key to store the result
  373. */
  374. static inline void key_read(const struct ubifs_info *c, const void *from,
  375. union ubifs_key *to)
  376. {
  377. const union ubifs_key *f = from;
  378. to->u32[0] = le32_to_cpu(f->j32[0]);
  379. to->u32[1] = le32_to_cpu(f->j32[1]);
  380. }
  381. /**
  382. * key_write - transform a key from in-memory format.
  383. * @c: UBIFS file-system description object
  384. * @from: the key to transform
  385. * @to: the key to store the result
  386. */
  387. static inline void key_write(const struct ubifs_info *c,
  388. const union ubifs_key *from, void *to)
  389. {
  390. union ubifs_key *t = to;
  391. t->j32[0] = cpu_to_le32(from->u32[0]);
  392. t->j32[1] = cpu_to_le32(from->u32[1]);
  393. memset(to + 8, 0, UBIFS_MAX_KEY_LEN - 8);
  394. }
  395. /**
  396. * key_write_idx - transform a key from in-memory format for the index.
  397. * @c: UBIFS file-system description object
  398. * @from: the key to transform
  399. * @to: the key to store the result
  400. */
  401. static inline void key_write_idx(const struct ubifs_info *c,
  402. const union ubifs_key *from, void *to)
  403. {
  404. union ubifs_key *t = to;
  405. t->j32[0] = cpu_to_le32(from->u32[0]);
  406. t->j32[1] = cpu_to_le32(from->u32[1]);
  407. }
  408. /**
  409. * key_copy - copy a key.
  410. * @c: UBIFS file-system description object
  411. * @from: the key to copy from
  412. * @to: the key to copy to
  413. */
  414. static inline void key_copy(const struct ubifs_info *c,
  415. const union ubifs_key *from, union ubifs_key *to)
  416. {
  417. to->u64[0] = from->u64[0];
  418. }
  419. /**
  420. * keys_cmp - compare keys.
  421. * @c: UBIFS file-system description object
  422. * @key1: the first key to compare
  423. * @key2: the second key to compare
  424. *
  425. * This function compares 2 keys and returns %-1 if @key1 is less than
  426. * @key2, %0 if the keys are equivalent and %1 if @key1 is greater than @key2.
  427. */
  428. static inline int keys_cmp(const struct ubifs_info *c,
  429. const union ubifs_key *key1,
  430. const union ubifs_key *key2)
  431. {
  432. if (key1->u32[0] < key2->u32[0])
  433. return -1;
  434. if (key1->u32[0] > key2->u32[0])
  435. return 1;
  436. if (key1->u32[1] < key2->u32[1])
  437. return -1;
  438. if (key1->u32[1] > key2->u32[1])
  439. return 1;
  440. return 0;
  441. }
  442. /**
  443. * keys_eq - determine if keys are equivalent.
  444. * @c: UBIFS file-system description object
  445. * @key1: the first key to compare
  446. * @key2: the second key to compare
  447. *
  448. * This function compares 2 keys and returns %1 if @key1 is equal to @key2 and
  449. * %0 if not.
  450. */
  451. static inline int keys_eq(const struct ubifs_info *c,
  452. const union ubifs_key *key1,
  453. const union ubifs_key *key2)
  454. {
  455. if (key1->u32[0] != key2->u32[0])
  456. return 0;
  457. if (key1->u32[1] != key2->u32[1])
  458. return 0;
  459. return 1;
  460. }
  461. /**
  462. * is_hash_key - is a key vulnerable to hash collisions.
  463. * @c: UBIFS file-system description object
  464. * @key: key
  465. *
  466. * This function returns %1 if @key is a hashed key or %0 otherwise.
  467. */
  468. static inline int is_hash_key(const struct ubifs_info *c,
  469. const union ubifs_key *key)
  470. {
  471. int type = key_type(c, key);
  472. return type == UBIFS_DENT_KEY || type == UBIFS_XENT_KEY;
  473. }
  474. /**
  475. * key_max_inode_size - get maximum file size allowed by current key format.
  476. * @c: UBIFS file-system description object
  477. */
  478. static inline unsigned long long key_max_inode_size(const struct ubifs_info *c)
  479. {
  480. switch (c->key_fmt) {
  481. case UBIFS_SIMPLE_KEY_FMT:
  482. return (1ULL << UBIFS_S_KEY_BLOCK_BITS) * UBIFS_BLOCK_SIZE;
  483. default:
  484. return 0;
  485. }
  486. }
  487. #endif /* !__UBIFS_KEY_H__ */