2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/types.h>
41 #include <sys/dirent.h>
48 * HAMMER2 offers shared locks and exclusive locks on inodes.
50 * An inode's ip->chain pointer is resolved and stable while an inode is
51 * locked, and can be cleaned out at any time (become NULL) when an inode
54 * The underlying chain is also locked and returned.
56 * NOTE: We don't combine the inode/chain lock because putting away an
57 * inode would otherwise confuse multiple lock holders of the inode.
60 hammer2_inode_lock_ex(hammer2_inode_t *ip)
62 hammer2_chain_t *chain;
64 hammer2_inode_ref(ip);
65 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
68 KKASSERT(chain != NULL); /* for now */
69 hammer2_chain_lock(ip->hmp, chain, HAMMER2_RESOLVE_ALWAYS);
75 hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_chain_t *chain)
78 * XXX this will catch parent directories too which we don't
81 if (ip->chain && (ip->chain->flags & (HAMMER2_CHAIN_MODIFIED |
82 HAMMER2_CHAIN_SUBMODIFIED))) {
83 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
86 hammer2_chain_unlock(ip->hmp, chain);
87 ccms_thread_unlock(&ip->topo_cst);
88 hammer2_inode_drop(ip);
92 * NOTE: We don't combine the inode/chain lock because putting away an
93 * inode would otherwise confuse multiple lock holders of the inode.
95 * Shared locks are especially sensitive to having too many shared
96 * lock counts (from the same thread) on certain paths which might
97 * need to upgrade them. Only one count of a shared lock can be
101 hammer2_inode_lock_sh(hammer2_inode_t *ip)
103 hammer2_chain_t *chain;
105 hammer2_inode_ref(ip);
106 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED);
109 KKASSERT(chain != NULL); /* for now */
110 hammer2_chain_lock(ip->hmp, chain, HAMMER2_RESOLVE_ALWAYS |
111 HAMMER2_RESOLVE_SHARED);
116 hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_chain_t *chain)
119 hammer2_chain_unlock(ip->hmp, chain);
120 ccms_thread_unlock(&ip->topo_cst);
121 hammer2_inode_drop(ip);
125 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
127 return(ccms_thread_lock_temp_release(&ip->topo_cst));
131 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
133 return(ccms_thread_lock_upgrade(&ip->topo_cst));
137 hammer2_inode_lock_restore(hammer2_inode_t *ip, ccms_state_t ostate)
139 ccms_thread_lock_restore(&ip->topo_cst, ostate);
147 hammer2_mount_exlock(hammer2_mount_t *hmp)
149 ccms_thread_lock(&hmp->vchain.cst, CCMS_STATE_EXCLUSIVE);
153 hammer2_mount_shlock(hammer2_mount_t *hmp)
155 ccms_thread_lock(&hmp->vchain.cst, CCMS_STATE_SHARED);
159 hammer2_mount_unlock(hammer2_mount_t *hmp)
161 ccms_thread_unlock(&hmp->vchain.cst);
165 hammer2_voldata_lock(hammer2_mount_t *hmp)
167 lockmgr(&hmp->voldatalk, LK_EXCLUSIVE);
171 hammer2_voldata_unlock(hammer2_mount_t *hmp)
173 lockmgr(&hmp->voldatalk, LK_RELEASE);
177 * Return the directory entry type for an inode.
179 * ip must be locked sh/ex.
182 hammer2_get_dtype(hammer2_chain_t *chain)
186 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
188 if ((type = chain->data->ipdata.type) == HAMMER2_OBJTYPE_HARDLINK)
189 type = chain->data->ipdata.target_type;
192 case HAMMER2_OBJTYPE_UNKNOWN:
194 case HAMMER2_OBJTYPE_DIRECTORY:
196 case HAMMER2_OBJTYPE_REGFILE:
198 case HAMMER2_OBJTYPE_FIFO:
200 case HAMMER2_OBJTYPE_CDEV: /* not supported */
202 case HAMMER2_OBJTYPE_BDEV: /* not supported */
204 case HAMMER2_OBJTYPE_SOFTLINK:
206 case HAMMER2_OBJTYPE_HARDLINK: /* (never directly associated w/vp) */
208 case HAMMER2_OBJTYPE_SOCKET:
210 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
219 * Return the directory entry type for an inode
222 hammer2_get_vtype(hammer2_chain_t *chain)
224 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
226 switch(chain->data->ipdata.type) {
227 case HAMMER2_OBJTYPE_UNKNOWN:
229 case HAMMER2_OBJTYPE_DIRECTORY:
231 case HAMMER2_OBJTYPE_REGFILE:
233 case HAMMER2_OBJTYPE_FIFO:
235 case HAMMER2_OBJTYPE_CDEV: /* not supported */
237 case HAMMER2_OBJTYPE_BDEV: /* not supported */
239 case HAMMER2_OBJTYPE_SOFTLINK:
241 case HAMMER2_OBJTYPE_HARDLINK: /* XXX */
243 case HAMMER2_OBJTYPE_SOCKET:
245 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
254 hammer2_get_obj_type(enum vtype vtype)
258 return(HAMMER2_OBJTYPE_DIRECTORY);
260 return(HAMMER2_OBJTYPE_REGFILE);
262 return(HAMMER2_OBJTYPE_FIFO);
264 return(HAMMER2_OBJTYPE_SOCKET);
266 return(HAMMER2_OBJTYPE_CDEV);
268 return(HAMMER2_OBJTYPE_BDEV);
270 return(HAMMER2_OBJTYPE_SOFTLINK);
272 return(HAMMER2_OBJTYPE_UNKNOWN);
278 * Convert a hammer2 64-bit time to a timespec.
281 hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts)
283 ts->tv_sec = (unsigned long)(xtime / 1000000);
284 ts->tv_nsec = (unsigned int)(xtime % 1000000) * 1000L;
288 hammer2_timespec_to_time(struct timespec *ts)
292 xtime = (unsigned)(ts->tv_nsec / 1000) +
293 (unsigned long)ts->tv_sec * 1000000ULL;
298 * Convert a uuid to a unix uid or gid
301 hammer2_to_unix_xid(uuid_t *uuid)
303 return(*(u_int32_t *)&uuid->node[2]);
307 hammer2_guid_to_uuid(uuid_t *uuid, u_int32_t guid)
309 bzero(uuid, sizeof(*uuid));
310 *(u_int32_t *)&uuid->node[2] = guid;
314 * Borrow HAMMER1's directory hash algorithm #1 with a few modifications.
315 * The filename is split into fields which are hashed separately and then
318 * Differences include: bit 63 must be set to 1 for HAMMER2 (HAMMER1 sets
319 * it to 0), this is because bit63=0 is used for hidden hardlinked inodes.
320 * (This means we do not need to do a 0-check/or-with-0x100000000 either).
322 * Also, the iscsi crc code is used instead of the old crc32 code.
325 hammer2_dirhash(const unsigned char *name, size_t len)
327 const unsigned char *aname = name;
339 for (i = j = 0; i < len; ++i) {
340 if (aname[i] == '.' ||
345 crcx += hammer2_icrc32(aname + j, i - j);
350 crcx += hammer2_icrc32(aname + j, i - j);
353 * The directory hash utilizes the top 32 bits of the 64-bit key.
354 * Bit 63 must be set to 1.
357 key |= (uint64_t)crcx << 32;
360 * l16 - crc of entire filename
362 * This crc reduces degenerate hash collision conditions
364 crcx = hammer2_icrc32(aname, len);
365 crcx = crcx ^ (crcx << 16);
366 key |= crcx & 0xFFFF0000U;
369 * Set bit 15. This allows readdir to strip bit 63 so a positive
370 * 64-bit cookie/offset can always be returned, and still guarantee
371 * that the values 0x0000-0x7FFF are available for artificial entries.
380 * Return the power-of-2 radix greater or equal to
381 * the specified number of bytes.
383 * Always returns at least the minimum media allocation
384 * size radix, HAMMER2_MIN_RADIX (10), which is 1KB.
387 hammer2_allocsize(size_t bytes)
391 if (bytes < HAMMER2_MIN_ALLOC)
392 bytes = HAMMER2_MIN_ALLOC;
393 if (bytes == HAMMER2_PBUFSIZE)
394 radix = HAMMER2_PBUFRADIX;
395 else if (bytes >= 16384)
397 else if (bytes >= 1024)
400 radix = HAMMER2_MIN_RADIX;
402 while (((size_t)1 << radix) < bytes)
408 * ip must be locked sh/ex
411 hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
412 hammer2_key_t *lbasep, hammer2_key_t *leofp)
414 hammer2_inode_data_t *ipdata = &ip->chain->data->ipdata;
417 *lbasep = uoff & ~HAMMER2_PBUFMASK64;
418 *leofp = ipdata->size & ~HAMMER2_PBUFMASK64;
419 KKASSERT(*lbasep <= *leofp);
420 if (*lbasep == *leofp /*&& *leofp < 1024 * 1024*/) {
421 radix = hammer2_allocsize((size_t)(ipdata->size - *leofp));
422 if (radix < HAMMER2_MINALLOCRADIX)
423 radix = HAMMER2_MINALLOCRADIX;
424 *leofp += 1U << radix;
425 return (1U << radix);
427 return (HAMMER2_PBUFSIZE);
432 hammer2_update_time(uint64_t *timep)
437 *timep = (unsigned long)tv.tv_sec * 1000000 + tv.tv_usec;