hash.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * fs/f2fs/hash.c
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * Portions of this code from linux/fs/ext3/hash.c
  8. *
  9. * Copyright (C) 2002 by Theodore Ts'o
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/types.h>
  16. #include <linux/fs.h>
  17. #include <linux/f2fs_fs.h>
  18. #include <linux/cryptohash.h>
  19. #include <linux/pagemap.h>
  20. #include "f2fs.h"
  21. /*
  22. * Hashing code copied from ext3
  23. */
  24. #define DELTA 0x9E3779B9
  25. static void TEA_transform(unsigned int buf[4], unsigned int const in[])
  26. {
  27. __u32 sum = 0;
  28. __u32 b0 = buf[0], b1 = buf[1];
  29. __u32 a = in[0], b = in[1], c = in[2], d = in[3];
  30. int n = 16;
  31. do {
  32. sum += DELTA;
  33. b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
  34. b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
  35. } while (--n);
  36. buf[0] += b0;
  37. buf[1] += b1;
  38. }
  39. static void str2hashbuf(const char *msg, size_t len, unsigned int *buf, int num)
  40. {
  41. unsigned pad, val;
  42. int i;
  43. pad = (__u32)len | ((__u32)len << 8);
  44. pad |= pad << 16;
  45. val = pad;
  46. if (len > num * 4)
  47. len = num * 4;
  48. for (i = 0; i < len; i++) {
  49. if ((i % 4) == 0)
  50. val = pad;
  51. val = msg[i] + (val << 8);
  52. if ((i % 4) == 3) {
  53. *buf++ = val;
  54. val = pad;
  55. num--;
  56. }
  57. }
  58. if (--num >= 0)
  59. *buf++ = val;
  60. while (--num >= 0)
  61. *buf++ = pad;
  62. }
  63. f2fs_hash_t f2fs_dentry_hash(const char *name, size_t len)
  64. {
  65. __u32 hash;
  66. f2fs_hash_t f2fs_hash;
  67. const char *p;
  68. __u32 in[8], buf[4];
  69. if ((len <= 2) && (name[0] == '.') &&
  70. (name[1] == '.' || name[1] == '\0'))
  71. return 0;
  72. /* Initialize the default seed for the hash checksum functions */
  73. buf[0] = 0x67452301;
  74. buf[1] = 0xefcdab89;
  75. buf[2] = 0x98badcfe;
  76. buf[3] = 0x10325476;
  77. p = name;
  78. while (1) {
  79. str2hashbuf(p, len, in, 4);
  80. TEA_transform(buf, in);
  81. p += 16;
  82. if (len <= 16)
  83. break;
  84. len -= 16;
  85. }
  86. hash = buf[0];
  87. f2fs_hash = cpu_to_le32(hash & ~F2FS_HASH_COL_BIT);
  88. return f2fs_hash;
  89. }