key.h 14 KB

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