2 * Copyright (c) 2011-2014 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>
50 hammer2_mount_exlock(hammer2_mount_t *hmp)
52 ccms_thread_lock(&hmp->vchain.core->cst, CCMS_STATE_EXCLUSIVE);
56 hammer2_mount_shlock(hammer2_mount_t *hmp)
58 ccms_thread_lock(&hmp->vchain.core->cst, CCMS_STATE_SHARED);
62 hammer2_mount_unlock(hammer2_mount_t *hmp)
64 ccms_thread_unlock(&hmp->vchain.core->cst);
68 hammer2_voldata_lock(hammer2_mount_t *hmp)
70 lockmgr(&hmp->voldatalk, LK_EXCLUSIVE);
74 hammer2_voldata_unlock(hammer2_mount_t *hmp, int modify)
77 (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) {
78 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
79 hammer2_chain_ref(&hmp->vchain);
81 lockmgr(&hmp->voldatalk, LK_RELEASE);
85 * Return the directory entry type for an inode.
87 * ip must be locked sh/ex.
90 hammer2_get_dtype(hammer2_inode_data_t *ipdata)
94 if ((type = ipdata->type) == HAMMER2_OBJTYPE_HARDLINK)
95 type = ipdata->target_type;
98 case HAMMER2_OBJTYPE_UNKNOWN:
100 case HAMMER2_OBJTYPE_DIRECTORY:
102 case HAMMER2_OBJTYPE_REGFILE:
104 case HAMMER2_OBJTYPE_FIFO:
106 case HAMMER2_OBJTYPE_CDEV: /* not supported */
108 case HAMMER2_OBJTYPE_BDEV: /* not supported */
110 case HAMMER2_OBJTYPE_SOFTLINK:
112 case HAMMER2_OBJTYPE_HARDLINK: /* (never directly associated w/vp) */
114 case HAMMER2_OBJTYPE_SOCKET:
116 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
125 * Return the directory entry type for an inode
128 hammer2_get_vtype(hammer2_inode_data_t *ipdata)
130 switch(ipdata->type) {
131 case HAMMER2_OBJTYPE_UNKNOWN:
133 case HAMMER2_OBJTYPE_DIRECTORY:
135 case HAMMER2_OBJTYPE_REGFILE:
137 case HAMMER2_OBJTYPE_FIFO:
139 case HAMMER2_OBJTYPE_CDEV: /* not supported */
141 case HAMMER2_OBJTYPE_BDEV: /* not supported */
143 case HAMMER2_OBJTYPE_SOFTLINK:
145 case HAMMER2_OBJTYPE_HARDLINK: /* XXX */
147 case HAMMER2_OBJTYPE_SOCKET:
149 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
158 hammer2_get_obj_type(enum vtype vtype)
162 return(HAMMER2_OBJTYPE_DIRECTORY);
164 return(HAMMER2_OBJTYPE_REGFILE);
166 return(HAMMER2_OBJTYPE_FIFO);
168 return(HAMMER2_OBJTYPE_SOCKET);
170 return(HAMMER2_OBJTYPE_CDEV);
172 return(HAMMER2_OBJTYPE_BDEV);
174 return(HAMMER2_OBJTYPE_SOFTLINK);
176 return(HAMMER2_OBJTYPE_UNKNOWN);
182 * Convert a hammer2 64-bit time to a timespec.
185 hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts)
187 ts->tv_sec = (unsigned long)(xtime / 1000000);
188 ts->tv_nsec = (unsigned int)(xtime % 1000000) * 1000L;
192 hammer2_timespec_to_time(struct timespec *ts)
196 xtime = (unsigned)(ts->tv_nsec / 1000) +
197 (unsigned long)ts->tv_sec * 1000000ULL;
202 * Convert a uuid to a unix uid or gid
205 hammer2_to_unix_xid(uuid_t *uuid)
207 return(*(u_int32_t *)&uuid->node[2]);
211 hammer2_guid_to_uuid(uuid_t *uuid, u_int32_t guid)
213 bzero(uuid, sizeof(*uuid));
214 *(u_int32_t *)&uuid->node[2] = guid;
218 * Borrow HAMMER1's directory hash algorithm #1 with a few modifications.
219 * The filename is split into fields which are hashed separately and then
222 * Differences include: bit 63 must be set to 1 for HAMMER2 (HAMMER1 sets
223 * it to 0), this is because bit63=0 is used for hidden hardlinked inodes.
224 * (This means we do not need to do a 0-check/or-with-0x100000000 either).
226 * Also, the iscsi crc code is used instead of the old crc32 code.
229 hammer2_dirhash(const unsigned char *name, size_t len)
231 const unsigned char *aname = name;
243 for (i = j = 0; i < len; ++i) {
244 if (aname[i] == '.' ||
249 crcx += hammer2_icrc32(aname + j, i - j);
254 crcx += hammer2_icrc32(aname + j, i - j);
257 * The directory hash utilizes the top 32 bits of the 64-bit key.
258 * Bit 63 must be set to 1.
261 key |= (uint64_t)crcx << 32;
264 * l16 - crc of entire filename
266 * This crc reduces degenerate hash collision conditions
268 crcx = hammer2_icrc32(aname, len);
269 crcx = crcx ^ (crcx << 16);
270 key |= crcx & 0xFFFF0000U;
273 * Set bit 15. This allows readdir to strip bit 63 so a positive
274 * 64-bit cookie/offset can always be returned, and still guarantee
275 * that the values 0x0000-0x7FFF are available for artificial entries.
285 * Return the power-of-2 radix greater or equal to
286 * the specified number of bytes.
288 * Always returns at least the minimum media allocation
289 * size radix, HAMMER2_MIN_RADIX (10), which is 1KB.
292 hammer2_allocsize(size_t bytes)
296 if (bytes < HAMMER2_MIN_ALLOC)
297 bytes = HAMMER2_MIN_ALLOC;
298 if (bytes == HAMMER2_PBUFSIZE)
299 radix = HAMMER2_PBUFRADIX;
300 else if (bytes >= 16384)
302 else if (bytes >= 1024)
305 radix = HAMMER2_MIN_RADIX;
307 while (((size_t)1 << radix) < bytes)
315 * Convert bytes to radix with no limitations
318 hammer2_getradix(size_t bytes)
322 if (bytes == HAMMER2_PBUFSIZE)
323 radix = HAMMER2_PBUFRADIX;
324 else if (bytes >= HAMMER2_LBUFSIZE)
325 radix = HAMMER2_LBUFRADIX;
326 else if (bytes >= HAMMER2_MIN_ALLOC) /* clamp */
327 radix = HAMMER2_MIN_RADIX;
331 while (((size_t)1 << radix) < bytes)
337 * ip must be locked sh/ex
339 * Use 16KB logical buffers for file blocks <= 1MB and 64KB logical buffers
340 * otherwise. The write code may utilize smaller device buffers when
341 * compressing or handling the EOF case, but is not able to coalesce smaller
342 * logical buffers into larger device buffers.
344 * For now this means that even large files will have a bunch of 16KB blocks
345 * at the beginning of the file. On the plus side this tends to cause small
346 * files to cluster together in the freemap.
349 hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
350 hammer2_key_t *lbasep, hammer2_key_t *leofp)
353 if (uoff < (hammer2_off_t)1024 * 1024) {
355 *lbasep = uoff & ~HAMMER2_LBUFMASK64;
357 if (ip->size > (hammer2_key_t)1024 * 1024)
358 *leofp = (hammer2_key_t)1024 * 1024;
360 *leofp = (ip->size + HAMMER2_LBUFMASK64) &
363 return (HAMMER2_LBUFSIZE);
367 *lbasep = uoff & ~HAMMER2_PBUFMASK64;
369 *leofp = (ip->size + HAMMER2_PBUFMASK64) &
372 return (HAMMER2_PBUFSIZE);
379 * Calculate the physical block size. pblksize <= lblksize. Primarily
380 * used to calculate a smaller physical block for the logical block
381 * containing the file EOF.
383 * Returns 0 if the requested base offset is beyond the file EOF.
386 hammer2_calc_physical(hammer2_inode_t *ip, hammer2_inode_data_t *ipdata,
393 lblksize = hammer2_calc_logical(ip, lbase, NULL, NULL);
394 if (lbase + lblksize <= ipdata->size)
396 if (lbase >= ipdata->size)
398 eofbytes = (int)(ipdata->size - lbase);
400 while (pblksize >= eofbytes && pblksize >= HAMMER2_MIN_ALLOC)
408 hammer2_update_time(uint64_t *timep)
413 *timep = (unsigned long)tv.tv_sec * 1000000 + tv.tv_usec;
417 hammer2_adjreadcounter(hammer2_blockref_t *bref, size_t bytes)
422 case HAMMER2_BREF_TYPE_DATA:
423 counterp = &hammer2_iod_file_read;
425 case HAMMER2_BREF_TYPE_INODE:
426 counterp = &hammer2_iod_meta_read;
428 case HAMMER2_BREF_TYPE_INDIRECT:
429 counterp = &hammer2_iod_indr_read;
431 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
432 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
433 counterp = &hammer2_iod_fmap_read;
436 counterp = &hammer2_iod_volu_read;