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>
41 #include <sys/dirent.h>
49 hammer2_dev_exlock(hammer2_dev_t *hmp)
51 hammer2_mtx_ex(&hmp->vchain.lock);
55 hammer2_dev_shlock(hammer2_dev_t *hmp)
57 hammer2_mtx_sh(&hmp->vchain.lock);
61 hammer2_dev_unlock(hammer2_dev_t *hmp)
63 hammer2_mtx_unlock(&hmp->vchain.lock);
67 * Return the directory entry type for an inode.
69 * ip must be locked sh/ex.
72 hammer2_get_dtype(uint8_t type)
75 case HAMMER2_OBJTYPE_UNKNOWN:
77 case HAMMER2_OBJTYPE_DIRECTORY:
79 case HAMMER2_OBJTYPE_REGFILE:
81 case HAMMER2_OBJTYPE_FIFO:
83 case HAMMER2_OBJTYPE_CDEV: /* not supported */
85 case HAMMER2_OBJTYPE_BDEV: /* not supported */
87 case HAMMER2_OBJTYPE_SOFTLINK:
89 case HAMMER2_OBJTYPE_SOCKET:
91 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
100 * Return the directory entry type for an inode
103 hammer2_get_vtype(uint8_t type)
106 case HAMMER2_OBJTYPE_UNKNOWN:
108 case HAMMER2_OBJTYPE_DIRECTORY:
110 case HAMMER2_OBJTYPE_REGFILE:
112 case HAMMER2_OBJTYPE_FIFO:
114 case HAMMER2_OBJTYPE_CDEV: /* not supported */
116 case HAMMER2_OBJTYPE_BDEV: /* not supported */
118 case HAMMER2_OBJTYPE_SOFTLINK:
120 case HAMMER2_OBJTYPE_SOCKET:
122 case HAMMER2_OBJTYPE_WHITEOUT: /* not supported */
131 hammer2_get_obj_type(enum vtype vtype)
135 return(HAMMER2_OBJTYPE_DIRECTORY);
137 return(HAMMER2_OBJTYPE_REGFILE);
139 return(HAMMER2_OBJTYPE_FIFO);
141 return(HAMMER2_OBJTYPE_SOCKET);
143 return(HAMMER2_OBJTYPE_CDEV);
145 return(HAMMER2_OBJTYPE_BDEV);
147 return(HAMMER2_OBJTYPE_SOFTLINK);
149 return(HAMMER2_OBJTYPE_UNKNOWN);
155 * Convert a hammer2 64-bit time to a timespec.
158 hammer2_time_to_timespec(uint64_t xtime, struct timespec *ts)
160 ts->tv_sec = (unsigned long)(xtime / 1000000);
161 ts->tv_nsec = (unsigned int)(xtime % 1000000) * 1000L;
165 hammer2_timespec_to_time(const struct timespec *ts)
169 xtime = (unsigned)(ts->tv_nsec / 1000) +
170 (unsigned long)ts->tv_sec * 1000000ULL;
175 * Convert a uuid to a unix uid or gid
178 hammer2_to_unix_xid(const uuid_t *uuid)
180 return(*(const uint32_t *)&uuid->node[2]);
184 hammer2_guid_to_uuid(uuid_t *uuid, uint32_t guid)
186 bzero(uuid, sizeof(*uuid));
187 *(uint32_t *)&uuid->node[2] = guid;
191 * Borrow HAMMER1's directory hash algorithm #1 with a few modifications.
192 * The filename is split into fields which are hashed separately and then
195 * Differences include: bit 63 must be set to 1 for HAMMER2 (HAMMER1 sets
196 * it to 0), this is because bit63=0 is used for hidden hardlinked inodes.
197 * (This means we do not need to do a 0-check/or-with-0x100000000 either).
199 * Also, the iscsi crc code is used instead of the old crc32 code.
202 hammer2_dirhash(const unsigned char *name, size_t len)
204 const unsigned char *aname = name;
216 for (i = j = 0; i < len; ++i) {
217 if (aname[i] == '.' ||
222 crcx += hammer2_icrc32(aname + j, i - j);
227 crcx += hammer2_icrc32(aname + j, i - j);
230 * The directory hash utilizes the top 32 bits of the 64-bit key.
231 * Bit 63 must be set to 1.
234 key |= (uint64_t)crcx << 32;
237 * l16 - crc of entire filename
239 * This crc reduces degenerate hash collision conditions
241 crcx = hammer2_icrc32(aname, len);
242 crcx = crcx ^ (crcx << 16);
243 key |= crcx & 0xFFFF0000U;
246 * Set bit 15. This allows readdir to strip bit 63 so a positive
247 * 64-bit cookie/offset can always be returned, and still guarantee
248 * that the values 0x0000-0x7FFF are available for artificial entries.
257 * Convert bytes to radix with no limitations.
259 * 0 bytes is special-cased to a radix of zero (which would normally
260 * translate to (1 << 0) == 1).
263 hammer2_getradix(size_t bytes)
268 * Optimize the iteration by pre-checking commonly used radii.
270 if (bytes == HAMMER2_PBUFSIZE)
271 radix = HAMMER2_PBUFRADIX;
272 else if (bytes >= HAMMER2_LBUFSIZE)
273 radix = HAMMER2_LBUFRADIX;
274 else if (bytes >= HAMMER2_ALLOC_MIN) /* clamp */
275 radix = HAMMER2_RADIX_MIN;
280 * Iterate as needed. Note that bytes == 0 is expected to return
281 * a radix of 0 as a special case.
283 while (((size_t)1 << radix) < bytes)
289 * The logical block size is currently always PBUFSIZE.
292 hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
293 hammer2_key_t *lbasep, hammer2_key_t *leofp)
295 KKASSERT(ip->flags & HAMMER2_INODE_METAGOOD);
297 *lbasep = uoff & ~HAMMER2_PBUFMASK64;
299 *leofp = (ip->meta.size + HAMMER2_PBUFMASK64) &
302 return (HAMMER2_PBUFSIZE);
306 * Calculate the physical block size. pblksize <= lblksize. Primarily
307 * used to calculate a smaller physical block for the logical block
308 * containing the file EOF.
310 * Returns 0 if the requested base offset is beyond the file EOF.
313 hammer2_calc_physical(hammer2_inode_t *ip, hammer2_key_t lbase)
319 KKASSERT(ip->flags & HAMMER2_INODE_METAGOOD);
320 lblksize = hammer2_calc_logical(ip, lbase, NULL, NULL);
321 if (lbase + lblksize <= ip->meta.size)
323 if (lbase >= ip->meta.size)
325 eofbytes = (int)(ip->meta.size - lbase);
327 while (pblksize >= eofbytes && pblksize >= HAMMER2_ALLOC_MIN)
335 hammer2_update_time(uint64_t *timep)
340 *timep = (unsigned long)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
344 hammer2_adjreadcounter(hammer2_blockref_t *bref, size_t bytes)
349 case HAMMER2_BREF_TYPE_DATA:
350 counterp = &hammer2_iod_file_read;
352 case HAMMER2_BREF_TYPE_DIRENT:
353 case HAMMER2_BREF_TYPE_INODE:
354 counterp = &hammer2_iod_meta_read;
356 case HAMMER2_BREF_TYPE_INDIRECT:
357 counterp = &hammer2_iod_indr_read;
359 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
360 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
361 counterp = &hammer2_iod_fmap_read;
363 case HAMMER2_BREF_TYPE_FREEMAP:
364 case HAMMER2_BREF_TYPE_VOLUME:
365 counterp = &hammer2_iod_volu_read;
367 case HAMMER2_BREF_TYPE_EMPTY:
375 * Check for pending signal to allow interruption. This function will
376 * return immediately if the calling thread is a kernel thread and not
380 hammer2_signal_check(time_t *timep)
382 thread_t td = curthread;
387 if (*timep != time_second) {
388 *timep = time_second;
389 if (CURSIG_NOBLOCK(curthread->td_lwp) != 0)
390 error = HAMMER2_ERROR_ABORTED;
399 hammer2_error_str(int error)
401 if (error & HAMMER2_ERROR_EIO)
403 if (error & HAMMER2_ERROR_CHECK)
404 return("Check Error");
405 if (error & HAMMER2_ERROR_INCOMPLETE)
406 return("Cluster Quorum Error");
407 if (error & HAMMER2_ERROR_DEPTH)
408 return("Chain Depth Error");
409 if (error & HAMMER2_ERROR_BADBREF)
410 return("Bad Blockref Error");
411 if (error & HAMMER2_ERROR_ENOSPC)
412 return("No Space on Device");
413 if (error & HAMMER2_ERROR_ENOENT)
414 return("Entry Not Found");
415 if (error & HAMMER2_ERROR_ENOTEMPTY)
416 return("Directory Not Empty");
417 if (error & HAMMER2_ERROR_EAGAIN)
419 if (error & HAMMER2_ERROR_ENOTDIR)
420 return("Not a Directory");
421 if (error & HAMMER2_ERROR_EISDIR)
422 return("Is a Directory");
423 if (error & HAMMER2_ERROR_EINPROGRESS)
424 return("Operation in Progress");
425 if (error & HAMMER2_ERROR_ABORTED)
426 return("Operation Aborted");
427 if (error & HAMMER2_ERROR_EOF)
428 return("Operation Complete");
429 if (error & HAMMER2_ERROR_EINVAL)
430 return("Invalid Operation");
431 if (error & HAMMER2_ERROR_EEXIST)
432 return("Object Exists");
433 if (error & HAMMER2_ERROR_EDEADLK)
434 return("Deadlock Detected");
435 if (error & HAMMER2_ERROR_ESRCH)
436 return("Object Not Found");
437 if (error & HAMMER2_ERROR_ETIMEDOUT)
439 return("Unknown Error");
443 hammer2_bref_type_str(hammer2_blockref_t *bref)
446 case HAMMER2_BREF_TYPE_EMPTY:
447 return("Unknown-zero'd field");
448 case HAMMER2_BREF_TYPE_INODE:
450 case HAMMER2_BREF_TYPE_INDIRECT:
451 return("Indirect-Block");
452 case HAMMER2_BREF_TYPE_DATA:
453 return("Data-Block");
454 case HAMMER2_BREF_TYPE_DIRENT:
455 return("Directory-Entry");
456 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
457 return("Freemap-Node");
458 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
459 return("Freemap-Leaf");
460 case HAMMER2_BREF_TYPE_FREEMAP:
461 return("Freemap-Header");
462 case HAMMER2_BREF_TYPE_VOLUME:
463 return("Volume-Header");