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