key.h 15 KB

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