key.h 15 KB

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