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 * Return the directory entry type for an inode.
70 * ip must be locked sh/ex.
73 hammer2_get_dtype(const hammer2_inode_data_t *ipdata)
77 if ((type = ipdata->type) == HAMMER2_OBJTYPE_HARDLINK)
78 type = ipdata->target_type;
81 case HAMMER2_OBJTYPE_UNKNOWN:
83 case HAMMER2_OBJTYPE_DIRECTORY:
85 case HAMMER2_OBJTYPE_REGFILE:
87 case HAMMER2_OBJTYPE_FIFO:
89 case HAMMER2_OBJTYPE_CDEV: /* not supported */
91 case HAMMER2_OBJTYPE_BDEV: /* not supported */
93 case HAMMER2_OBJTYPE_SOFTLINK:
95 case HAMMER2_OBJTYPE_HARDLINK: /* (never directly associated w/vp) */
97 case HAMMER2_OBJTYPE_SOCKET:
99 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
108 * Return the directory entry type for an inode
111 hammer2_get_vtype(const hammer2_inode_data_t *ipdata)
113 switch(ipdata->type) {
114 case HAMMER2_OBJTYPE_UNKNOWN:
116 case HAMMER2_OBJTYPE_DIRECTORY:
118 case HAMMER2_OBJTYPE_REGFILE:
120 case HAMMER2_OBJTYPE_FIFO:
122 case HAMMER2_OBJTYPE_CDEV: /* not supported */
124 case HAMMER2_OBJTYPE_BDEV: /* not supported */
126 case HAMMER2_OBJTYPE_SOFTLINK:
128 case HAMMER2_OBJTYPE_HARDLINK: /* XXX */
130 case HAMMER2_OBJTYPE_SOCKET:
132 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
141 hammer2_get_obj_type(enum vtype vtype)
145 return(HAMMER2_OBJTYPE_DIRECTORY);
147 return(HAMMER2_OBJTYPE_REGFILE);
149 return(HAMMER2_OBJTYPE_FIFO);
151 return(HAMMER2_OBJTYPE_SOCKET);
153 return(HAMMER2_OBJTYPE_CDEV);
155 return(HAMMER2_OBJTYPE_BDEV);
157 return(HAMMER2_OBJTYPE_SOFTLINK);
159 return(HAMMER2_OBJTYPE_UNKNOWN);
165 * Convert a hammer2 64-bit time to a timespec.
168 hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts)
170 ts->tv_sec = (unsigned long)(xtime / 1000000);
171 ts->tv_nsec = (unsigned int)(xtime % 1000000) * 1000L;
175 hammer2_timespec_to_time(const struct timespec *ts)
179 xtime = (unsigned)(ts->tv_nsec / 1000) +
180 (unsigned long)ts->tv_sec * 1000000ULL;
185 * Convert a uuid to a unix uid or gid
188 hammer2_to_unix_xid(const uuid_t *uuid)
190 return(*(const u_int32_t *)&uuid->node[2]);
194 hammer2_guid_to_uuid(uuid_t *uuid, u_int32_t guid)
196 bzero(uuid, sizeof(*uuid));
197 *(u_int32_t *)&uuid->node[2] = guid;
201 * Borrow HAMMER1's directory hash algorithm #1 with a few modifications.
202 * The filename is split into fields which are hashed separately and then
205 * Differences include: bit 63 must be set to 1 for HAMMER2 (HAMMER1 sets
206 * it to 0), this is because bit63=0 is used for hidden hardlinked inodes.
207 * (This means we do not need to do a 0-check/or-with-0x100000000 either).
209 * Also, the iscsi crc code is used instead of the old crc32 code.
212 hammer2_dirhash(const unsigned char *name, size_t len)
214 const unsigned char *aname = name;
226 for (i = j = 0; i < len; ++i) {
227 if (aname[i] == '.' ||
232 crcx += hammer2_icrc32(aname + j, i - j);
237 crcx += hammer2_icrc32(aname + j, i - j);
240 * The directory hash utilizes the top 32 bits of the 64-bit key.
241 * Bit 63 must be set to 1.
244 key |= (uint64_t)crcx << 32;
247 * l16 - crc of entire filename
249 * This crc reduces degenerate hash collision conditions
251 crcx = hammer2_icrc32(aname, len);
252 crcx = crcx ^ (crcx << 16);
253 key |= crcx & 0xFFFF0000U;
256 * Set bit 15. This allows readdir to strip bit 63 so a positive
257 * 64-bit cookie/offset can always be returned, and still guarantee
258 * that the values 0x0000-0x7FFF are available for artificial entries.
268 * Return the power-of-2 radix greater or equal to
269 * the specified number of bytes.
271 * Always returns at least the minimum media allocation
272 * size radix, HAMMER2_RADIX_MIN (10), which is 1KB.
275 hammer2_allocsize(size_t bytes)
279 if (bytes < HAMMER2_ALLOC_MIN)
280 bytes = HAMMER2_ALLOC_MIN;
281 if (bytes == HAMMER2_PBUFSIZE)
282 radix = HAMMER2_PBUFRADIX;
283 else if (bytes >= 16384)
285 else if (bytes >= 1024)
288 radix = HAMMER2_RADIX_MIN;
290 while (((size_t)1 << radix) < bytes)
298 * Convert bytes to radix with no limitations
301 hammer2_getradix(size_t bytes)
305 if (bytes == HAMMER2_PBUFSIZE)
306 radix = HAMMER2_PBUFRADIX;
307 else if (bytes >= HAMMER2_LBUFSIZE)
308 radix = HAMMER2_LBUFRADIX;
309 else if (bytes >= HAMMER2_ALLOC_MIN) /* clamp */
310 radix = HAMMER2_RADIX_MIN;
314 while (((size_t)1 << radix) < bytes)
320 * ip must be locked sh/ex
322 * Use 16KB logical buffers for file blocks <= 1MB and 64KB logical buffers
323 * otherwise. The write code may utilize smaller device buffers when
324 * compressing or handling the EOF case, but is not able to coalesce smaller
325 * logical buffers into larger device buffers.
327 * For now this means that even large files will have a bunch of 16KB blocks
328 * at the beginning of the file. On the plus side this tends to cause small
329 * files to cluster together in the freemap.
332 hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
333 hammer2_key_t *lbasep, hammer2_key_t *leofp)
336 if (uoff < (hammer2_off_t)1024 * 1024) {
338 *lbasep = uoff & ~HAMMER2_LBUFMASK64;
340 if (ip->size > (hammer2_key_t)1024 * 1024)
341 *leofp = (hammer2_key_t)1024 * 1024;
343 *leofp = (ip->size + HAMMER2_LBUFMASK64) &
346 return (HAMMER2_LBUFSIZE);
350 *lbasep = uoff & ~HAMMER2_PBUFMASK64;
352 *leofp = (ip->size + HAMMER2_PBUFMASK64) &
355 return (HAMMER2_PBUFSIZE);
362 * Calculate the physical block size. pblksize <= lblksize. Primarily
363 * used to calculate a smaller physical block for the logical block
364 * containing the file EOF.
366 * Returns 0 if the requested base offset is beyond the file EOF.
369 hammer2_calc_physical(hammer2_inode_t *ip,
370 const hammer2_inode_data_t *ipdata,
377 lblksize = hammer2_calc_logical(ip, lbase, NULL, NULL);
378 if (lbase + lblksize <= ipdata->size)
380 if (lbase >= ipdata->size)
382 eofbytes = (int)(ipdata->size - lbase);
384 while (pblksize >= eofbytes && pblksize >= HAMMER2_ALLOC_MIN)
392 hammer2_update_time(uint64_t *timep)
397 *timep = (unsigned long)tv.tv_sec * 1000000 + tv.tv_usec;
401 hammer2_adjreadcounter(hammer2_blockref_t *bref, size_t bytes)
406 case HAMMER2_BREF_TYPE_DATA:
407 counterp = &hammer2_iod_file_read;
409 case HAMMER2_BREF_TYPE_INODE:
410 counterp = &hammer2_iod_meta_read;
412 case HAMMER2_BREF_TYPE_INDIRECT:
413 counterp = &hammer2_iod_indr_read;
415 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
416 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
417 counterp = &hammer2_iod_fmap_read;
420 counterp = &hammer2_iod_volu_read;
427 hammer2_signal_check(time_t *timep)
432 if (*timep != time_second) {
433 *timep = time_second;
434 if (CURSIG(curthread->td_lwp) != 0)