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, update locks, and exclusive locks on inodes.
50 * Shared locks allow concurrent access to an inode's fields, but exclude
51 * access by concurrent exclusive locks.
53 * Update locks are interesting -- an update lock will be taken after all
54 * shared locks on an inode are released, but once it is in place, shared
55 * locks may proceed. The update field is signalled by a busy flag in the
56 * inode. Only one update lock may be in place at a given time on an inode.
58 * Exclusive locks prevent concurrent access to the inode.
60 * XXX: What do we use each for? How is visibility to the inode controlled?
65 hammer2_inode_lock_ex(hammer2_inode_t *ip)
67 hammer2_chain_lock(ip->hmp, &ip->chain, HAMMER2_RESOLVE_ALWAYS);
71 hammer2_inode_unlock_ex(hammer2_inode_t *ip)
73 hammer2_chain_unlock(ip->hmp, &ip->chain);
77 hammer2_inode_lock_sh(hammer2_inode_t *ip)
79 KKASSERT(ip->chain.refs > 0);
80 hammer2_chain_lock(ip->hmp, &ip->chain, HAMMER2_RESOLVE_ALWAYS |
81 HAMMER2_RESOLVE_SHARED);
85 hammer2_inode_unlock_sh(hammer2_inode_t *ip)
87 hammer2_chain_unlock(ip->hmp, &ip->chain);
94 * The inode must be exclusively locked while soft-busying or soft-unbusying
95 * an inode. Once busied or unbusied the caller can release the lock.
98 hammer2_inode_busy(hammer2_inode_t *ip)
100 if (ip->chain.busy++ == 0)
101 hammer2_chain_ref(ip->hmp, &ip->chain, 0);
105 hammer2_inode_unbusy(hammer2_inode_t *ip)
107 if (--ip->chain.busy == 0)
108 hammer2_chain_drop(ip->hmp, &ip->chain);
118 hammer2_mount_exlock(hammer2_mount_t *hmp)
120 ccms_thread_lock(&hmp->vchain.cst, CCMS_STATE_EXCLUSIVE);
124 hammer2_mount_shlock(hammer2_mount_t *hmp)
126 ccms_thread_lock(&hmp->vchain.cst, CCMS_STATE_SHARED);
130 hammer2_mount_unlock(hammer2_mount_t *hmp)
132 ccms_thread_unlock(&hmp->vchain.cst);
136 hammer2_voldata_lock(hammer2_mount_t *hmp)
138 lockmgr(&hmp->voldatalk, LK_EXCLUSIVE);
142 hammer2_voldata_unlock(hammer2_mount_t *hmp)
144 lockmgr(&hmp->voldatalk, LK_RELEASE);
148 * Return the directory entry type for an inode
151 hammer2_get_dtype(hammer2_inode_t *ip)
155 if ((type = ip->ip_data.type) == HAMMER2_OBJTYPE_HARDLINK)
156 type = ip->ip_data.target_type;
159 case HAMMER2_OBJTYPE_UNKNOWN:
161 case HAMMER2_OBJTYPE_DIRECTORY:
163 case HAMMER2_OBJTYPE_REGFILE:
165 case HAMMER2_OBJTYPE_FIFO:
167 case HAMMER2_OBJTYPE_CDEV: /* not supported */
169 case HAMMER2_OBJTYPE_BDEV: /* not supported */
171 case HAMMER2_OBJTYPE_SOFTLINK:
173 case HAMMER2_OBJTYPE_HARDLINK: /* (never directly associated w/vp) */
175 case HAMMER2_OBJTYPE_SOCKET:
177 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
186 * Return the directory entry type for an inode
189 hammer2_get_vtype(hammer2_inode_t *ip)
191 switch(ip->ip_data.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: /* XXX */
208 case HAMMER2_OBJTYPE_SOCKET:
210 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
219 hammer2_get_obj_type(enum vtype vtype)
223 return(HAMMER2_OBJTYPE_DIRECTORY);
225 return(HAMMER2_OBJTYPE_REGFILE);
227 return(HAMMER2_OBJTYPE_FIFO);
229 return(HAMMER2_OBJTYPE_SOCKET);
231 return(HAMMER2_OBJTYPE_CDEV);
233 return(HAMMER2_OBJTYPE_BDEV);
235 return(HAMMER2_OBJTYPE_SOFTLINK);
237 return(HAMMER2_OBJTYPE_UNKNOWN);
243 * Convert a hammer2 64-bit time to a timespec.
246 hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts)
248 ts->tv_sec = (unsigned long)(xtime / 1000000);
249 ts->tv_nsec = (unsigned int)(xtime % 1000000) * 1000L;
253 hammer2_timespec_to_time(struct timespec *ts)
257 xtime = (unsigned)(ts->tv_nsec / 1000) +
258 (unsigned long)ts->tv_sec * 1000000ULL;
263 * Convert a uuid to a unix uid or gid
266 hammer2_to_unix_xid(uuid_t *uuid)
268 return(*(u_int32_t *)&uuid->node[2]);
272 hammer2_guid_to_uuid(uuid_t *uuid, u_int32_t guid)
274 bzero(uuid, sizeof(*uuid));
275 *(u_int32_t *)&uuid->node[2] = guid;
279 * Borrow HAMMER1's directory hash algorithm #1 with a few modifications.
280 * The filename is split into fields which are hashed separately and then
283 * Differences include: bit 63 must be set to 1 for HAMMER2 (HAMMER1 sets
284 * it to 0), this is because bit63=0 is used for hidden hardlinked inodes.
285 * (This means we do not need to do a 0-check/or-with-0x100000000 either).
287 * Also, the iscsi crc code is used instead of the old crc32 code.
290 hammer2_dirhash(const unsigned char *name, size_t len)
292 const unsigned char *aname = name;
304 for (i = j = 0; i < len; ++i) {
305 if (aname[i] == '.' ||
310 crcx += hammer2_icrc32(aname + j, i - j);
315 crcx += hammer2_icrc32(aname + j, i - j);
318 * The directory hash utilizes the top 32 bits of the 64-bit key.
319 * Bit 63 must be set to 1.
322 key |= (uint64_t)crcx << 32;
325 * l16 - crc of entire filename
327 * This crc reduces degenerate hash collision conditions
329 crcx = hammer2_icrc32(aname, len);
330 crcx = crcx ^ (crcx << 16);
331 key |= crcx & 0xFFFF0000U;
334 * Set bit 15. This allows readdir to strip bit 63 so a positive
335 * 64-bit cookie/offset can always be returned, and still guarantee
336 * that the values 0x0000-0x7FFF are available for artificial entries.
345 * Return the power-of-2 radix greater or equal to
346 * the specified number of bytes.
348 * Always returns at least HAMMER2_MIN_RADIX (2^6).
351 hammer2_bytes_to_radix(size_t bytes)
355 if (bytes < HAMMER2_MIN_ALLOC)
356 bytes = HAMMER2_MIN_ALLOC;
357 if (bytes == HAMMER2_PBUFSIZE)
358 radix = HAMMER2_PBUFRADIX;
359 else if (bytes >= 1024)
362 radix = HAMMER2_MIN_RADIX;
364 while (((size_t)1 << radix) < bytes)
370 hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
371 hammer2_key_t *lbasep, hammer2_key_t *leofp)
375 *lbasep = uoff & ~HAMMER2_PBUFMASK64;
376 *leofp = ip->ip_data.size & ~HAMMER2_PBUFMASK64;
377 KKASSERT(*lbasep <= *leofp);
378 if (*lbasep == *leofp /*&& *leofp < 1024 * 1024*/) {
379 radix = hammer2_bytes_to_radix(
380 (size_t)(ip->ip_data.size - *leofp));
381 if (radix < HAMMER2_MINALLOCRADIX)
382 radix = HAMMER2_MINALLOCRADIX;
383 *leofp += 1U << radix;
384 return (1U << radix);
386 return (HAMMER2_PBUFSIZE);
391 hammer2_update_time(uint64_t *timep)
396 *timep = (unsigned long)tv.tv_sec * 1000000 + tv.tv_usec;