hammer2_subr.c (5cde927f) hammer2_subr.c (50e4f8f4)
1/*
2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 269 unchanged lines hidden (view full) ---

278 hammer2_mount_t *hmp = ip->hmp;
279
280 KKASSERT(ip->hmp != NULL);
281 KKASSERT(ip->vp == NULL);
282 KKASSERT(ip->refs == 1);
283 hammer2_inode_unlock_ex(ip);
284 kfree(ip, hmp->inodes);
285}
1/*
2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 269 unchanged lines hidden (view full) ---

278 hammer2_mount_t *hmp = ip->hmp;
279
280 KKASSERT(ip->hmp != NULL);
281 KKASSERT(ip->vp == NULL);
282 KKASSERT(ip->refs == 1);
283 hammer2_inode_unlock_ex(ip);
284 kfree(ip, hmp->inodes);
285}
286
287/*
288 * Borrow HAMMER1's directory hash algorithm #1 with a few modifications.
289 * The filename is split into fields which are hashed separately and then
290 * added together.
291 *
292 * Differences include: bit 63 must be set to 1 for HAMMER2 (HAMMER1 sets
293 * it to 0), this is because bit63=0 is used for hidden hardlinked inodes.
294 * (This means we do not need to do a 0-check/or-with-0x100000000 either).
295 *
296 * Also, the iscsi crc code is used instead of the old crc32 code.
297 */
298hammer2_key_t
299hammer2_dirhash(const unsigned char *name, size_t len)
300{
301 const unsigned char *aname = name;
302 uint32_t crcx;
303 uint64_t key;
304 size_t i;
305 size_t j;
306
307 /*
308 * Filesystem version 6 or better will create directories
309 * using the ALG1 dirhash. This hash breaks the filename
310 * up into domains separated by special characters and
311 * hashes each domain independently.
312 *
313 * We also do a simple sub-sort using the first character
314 * of the filename in the top 5-bits.
315 */
316 key = 0;
317
318 /*
319 * m32
320 */
321 crcx = 0;
322 for (i = j = 0; i < len; ++i) {
323 if (aname[i] == '.' ||
324 aname[i] == '-' ||
325 aname[i] == '_' ||
326 aname[i] == '~') {
327 if (i != j)
328 crcx += hammer2_icrc32(aname + j, i - j);
329 j = i + 1;
330 }
331 }
332 if (i != j)
333 crcx += hammer2_icrc32(aname + j, i - j);
334
335 /*
336 * The directory hash utilizes the top 32 bits of the 64-bit key.
337 * Bit 63 must be set to 1.
338 */
339 crcx |= 0x80000000U;
340 key |= (uint64_t)crcx << 32;
341
342 /*
343 * l16 - crc of entire filename
344 *
345 * This crc reduces degenerate hash collision conditions
346 */
347 crcx = hammer2_icrc32(aname, len);
348 crcx = crcx ^ (crcx << 16);
349 key |= crcx & 0xFFFF0000U;
350
351 return (key);
352}