hash.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 unsigned char *msg, size_t len,
  40. unsigned int *buf, int num)
  41. {
  42. unsigned pad, val;
  43. int i;
  44. pad = (__u32)len | ((__u32)len << 8);
  45. pad |= pad << 16;
  46. val = pad;
  47. if (len > num * 4)
  48. len = num * 4;
  49. for (i = 0; i < len; i++) {
  50. if ((i % 4) == 0)
  51. val = pad;
  52. val = msg[i] + (val << 8);
  53. if ((i % 4) == 3) {
  54. *buf++ = val;
  55. val = pad;
  56. num--;
  57. }
  58. }
  59. if (--num >= 0)
  60. *buf++ = val;
  61. while (--num >= 0)
  62. *buf++ = pad;
  63. }
  64. f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
  65. struct f2fs_filename *fname)
  66. {
  67. __u32 hash;
  68. f2fs_hash_t f2fs_hash;
  69. const unsigned char *p;
  70. __u32 in[8], buf[4];
  71. const unsigned char *name = name_info->name;
  72. size_t len = name_info->len;
  73. /* encrypted bigname case */
  74. if (fname && !fname->disk_name.name)
  75. return cpu_to_le32(fname->hash);
  76. if (is_dot_dotdot(name_info))
  77. return 0;
  78. /* Initialize the default seed for the hash checksum functions */
  79. buf[0] = 0x67452301;
  80. buf[1] = 0xefcdab89;
  81. buf[2] = 0x98badcfe;
  82. buf[3] = 0x10325476;
  83. p = name;
  84. while (1) {
  85. str2hashbuf(p, len, in, 4);
  86. TEA_transform(buf, in);
  87. p += 16;
  88. if (len <= 16)
  89. break;
  90. len -= 16;
  91. }
  92. hash = buf[0];
  93. f2fs_hash = cpu_to_le32(hash & ~F2FS_HASH_COL_BIT);
  94. return f2fs_hash;
  95. }