2 * Copyright (c) 2011-2018 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>
40 #include <sys/dirent.h>
45 * Return the directory entry type for an inode.
48 hammer2_get_dtype(uint8_t type)
51 case HAMMER2_OBJTYPE_UNKNOWN:
53 case HAMMER2_OBJTYPE_DIRECTORY:
55 case HAMMER2_OBJTYPE_REGFILE:
57 case HAMMER2_OBJTYPE_FIFO:
59 case HAMMER2_OBJTYPE_CDEV:
61 case HAMMER2_OBJTYPE_BDEV:
63 case HAMMER2_OBJTYPE_SOFTLINK:
65 case HAMMER2_OBJTYPE_SOCKET:
67 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
76 * Return the directory entry type for an inode
79 hammer2_get_vtype(uint8_t type)
82 case HAMMER2_OBJTYPE_UNKNOWN:
84 case HAMMER2_OBJTYPE_DIRECTORY:
86 case HAMMER2_OBJTYPE_REGFILE:
88 case HAMMER2_OBJTYPE_FIFO:
90 case HAMMER2_OBJTYPE_CDEV:
92 case HAMMER2_OBJTYPE_BDEV:
94 case HAMMER2_OBJTYPE_SOFTLINK:
96 case HAMMER2_OBJTYPE_SOCKET:
98 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
107 hammer2_get_obj_type(enum vtype vtype)
111 return(HAMMER2_OBJTYPE_DIRECTORY);
113 return(HAMMER2_OBJTYPE_REGFILE);
115 return(HAMMER2_OBJTYPE_FIFO);
117 return(HAMMER2_OBJTYPE_SOCKET);
119 return(HAMMER2_OBJTYPE_CDEV);
121 return(HAMMER2_OBJTYPE_BDEV);
123 return(HAMMER2_OBJTYPE_SOFTLINK);
125 return(HAMMER2_OBJTYPE_UNKNOWN);
131 * Convert a hammer2 64-bit time to a timespec.
134 hammer2_time_to_timespec(uint64_t xtime, struct timespec *ts)
136 ts->tv_sec = (unsigned long)(xtime / 1000000);
137 ts->tv_nsec = (unsigned int)(xtime % 1000000) * 1000L;
141 hammer2_timespec_to_time(const struct timespec *ts)
145 xtime = (unsigned)(ts->tv_nsec / 1000) +
146 (unsigned long)ts->tv_sec * 1000000ULL;
151 * Convert a uuid to a unix uid or gid
154 hammer2_to_unix_xid(const uuid_t *uuid)
156 return(*(const uint32_t *)&uuid->node[2]);
160 hammer2_guid_to_uuid(uuid_t *uuid, uint32_t guid)
162 bzero(uuid, sizeof(*uuid));
163 *(uint32_t *)&uuid->node[2] = guid;
167 * Borrow HAMMER1's directory hash algorithm #1 with a few modifications.
168 * The filename is split into fields which are hashed separately and then
171 * Differences include: bit 63 must be set to 1 for HAMMER2 (HAMMER1 sets
172 * it to 0), this is because bit63=0 is used for hidden hardlinked inodes.
173 * (This means we do not need to do a 0-check/or-with-0x100000000 either).
175 * Also, the iscsi crc code is used instead of the old crc32 code.
178 hammer2_dirhash(const char *aname, size_t len)
191 for (i = j = 0; i < len; ++i) {
192 if (aname[i] == '.' ||
197 crcx += hammer2_icrc32(aname + j, i - j);
202 crcx += hammer2_icrc32(aname + j, i - j);
205 * The directory hash utilizes the top 32 bits of the 64-bit key.
206 * Bit 63 must be set to 1.
209 key |= (uint64_t)crcx << 32;
212 * l16 - crc of entire filename
214 * This crc reduces degenerate hash collision conditions.
216 crcx = hammer2_icrc32(aname, len);
217 crcx = crcx ^ (crcx << 16);
218 key |= crcx & 0xFFFF0000U;
221 * Set bit 15. This allows readdir to strip bit 63 so a positive
222 * 64-bit cookie/offset can always be returned, and still guarantee
223 * that the values 0x0000-0x7FFF are available for artificial entries.
232 * Convert bytes to radix with no limitations.
234 * 0 bytes is special-cased to a radix of zero (which would normally
235 * translate to (1 << 0) == 1).
238 hammer2_getradix(size_t bytes)
243 * Optimize the iteration by pre-checking commonly used radixes.
245 if (bytes == HAMMER2_PBUFSIZE)
246 radix = HAMMER2_PBUFRADIX;
247 else if (bytes >= HAMMER2_LBUFSIZE)
248 radix = HAMMER2_LBUFRADIX;
249 else if (bytes >= HAMMER2_ALLOC_MIN) /* clamp */
250 radix = HAMMER2_RADIX_MIN;
255 * Iterate as needed. Note that bytes == 0 is expected to return
256 * a radix of 0 as a special case.
258 while (((size_t)1 << radix) < bytes)
264 * The logical block size is currently always PBUFSIZE.
267 hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
268 hammer2_key_t *lbasep, hammer2_key_t *leofp)
271 *lbasep = uoff & ~HAMMER2_PBUFMASK64;
273 *leofp = (ip->meta.size + HAMMER2_PBUFMASK64) &
276 return (HAMMER2_PBUFSIZE);
280 * Calculate the physical block size. pblksize <= lblksize. Primarily
281 * used to calculate a smaller physical block for the logical block
282 * containing the file EOF.
284 * Returns 0 if the requested base offset is beyond the file EOF.
287 hammer2_calc_physical(hammer2_inode_t *ip, hammer2_key_t lbase)
293 lblksize = hammer2_calc_logical(ip, lbase, NULL, NULL);
294 if (lbase + lblksize <= ip->meta.size)
296 if (lbase >= ip->meta.size)
298 eofbytes = (int)(ip->meta.size - lbase);
300 while (pblksize >= eofbytes && pblksize >= HAMMER2_ALLOC_MIN)
308 hammer2_update_time(uint64_t *timep)
313 *timep = (unsigned long)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
317 hammer2_adjreadcounter(int btype, size_t bytes)
322 case HAMMER2_BREF_TYPE_DATA:
323 counterp = &hammer2_iod_file_read;
325 case HAMMER2_BREF_TYPE_DIRENT:
326 case HAMMER2_BREF_TYPE_INODE:
327 counterp = &hammer2_iod_meta_read;
329 case HAMMER2_BREF_TYPE_INDIRECT:
330 counterp = &hammer2_iod_indr_read;
332 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
333 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
334 counterp = &hammer2_iod_fmap_read;
336 case HAMMER2_BREF_TYPE_FREEMAP:
337 case HAMMER2_BREF_TYPE_VOLUME:
338 counterp = &hammer2_iod_volu_read;
340 case HAMMER2_BREF_TYPE_EMPTY:
348 hammer2_adjwritecounter(int btype, size_t bytes)
353 case HAMMER2_BREF_TYPE_DATA:
354 counterp = &hammer2_iod_file_write;
356 case HAMMER2_BREF_TYPE_DIRENT:
357 case HAMMER2_BREF_TYPE_INODE:
358 counterp = &hammer2_iod_meta_write;
360 case HAMMER2_BREF_TYPE_INDIRECT:
361 counterp = &hammer2_iod_indr_write;
363 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
364 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
365 counterp = &hammer2_iod_fmap_write;
367 case HAMMER2_BREF_TYPE_FREEMAP:
368 case HAMMER2_BREF_TYPE_VOLUME:
369 counterp = &hammer2_iod_volu_write;
371 case HAMMER2_BREF_TYPE_EMPTY:
379 * Check for pending signal to allow interruption. This function will
380 * return immediately if the calling thread is a kernel thread and not
384 hammer2_signal_check(time_t *timep)
386 thread_t td = curthread;
391 if (*timep != time_second) {
392 *timep = time_second;
393 if (CURSIG_NOBLOCK(curthread->td_lwp) != 0)
394 error = HAMMER2_ERROR_ABORTED;
403 hammer2_error_str(int error)
405 if (error & HAMMER2_ERROR_EIO)
407 if (error & HAMMER2_ERROR_CHECK)
408 return("Check Error");
409 if (error & HAMMER2_ERROR_INCOMPLETE)
410 return("Cluster Quorum Error");
411 if (error & HAMMER2_ERROR_DEPTH)
412 return("Chain Depth Error");
413 if (error & HAMMER2_ERROR_BADBREF)
414 return("Bad Blockref Error");
415 if (error & HAMMER2_ERROR_ENOSPC)
416 return("No Space on Device");
417 if (error & HAMMER2_ERROR_ENOENT)
418 return("Entry Not Found");
419 if (error & HAMMER2_ERROR_ENOTEMPTY)
420 return("Directory Not Empty");
421 if (error & HAMMER2_ERROR_EAGAIN)
423 if (error & HAMMER2_ERROR_ENOTDIR)
424 return("Not a Directory");
425 if (error & HAMMER2_ERROR_EISDIR)
426 return("Is a Directory");
427 if (error & HAMMER2_ERROR_EINPROGRESS)
428 return("Operation in Progress");
429 if (error & HAMMER2_ERROR_ABORTED)
430 return("Operation Aborted");
431 if (error & HAMMER2_ERROR_EOF)
432 return("Operation Complete");
433 if (error & HAMMER2_ERROR_EINVAL)
434 return("Invalid Operation");
435 if (error & HAMMER2_ERROR_EEXIST)
436 return("Object Exists");
437 if (error & HAMMER2_ERROR_EDEADLK)
438 return("Deadlock Detected");
439 if (error & HAMMER2_ERROR_ESRCH)
440 return("Object Not Found");
441 if (error & HAMMER2_ERROR_ETIMEDOUT)
443 return("Unknown Error");
447 hammer2_bref_type_str(int btype)
450 case HAMMER2_BREF_TYPE_EMPTY:
452 case HAMMER2_BREF_TYPE_INODE:
454 case HAMMER2_BREF_TYPE_INDIRECT:
456 case HAMMER2_BREF_TYPE_DATA:
458 case HAMMER2_BREF_TYPE_DIRENT:
460 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
461 return("freemap_node");
462 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
463 return("freemap_leaf");
464 case HAMMER2_BREF_TYPE_INVALID:
466 case HAMMER2_BREF_TYPE_FREEMAP:
468 case HAMMER2_BREF_TYPE_VOLUME: