xref: /dragonfly/sys/vfs/hammer2/hammer2_subr.c (revision 48e07bd5)
1 /*
2  * Copyright (c) 2011-2018 The DragonFly Project.  All rights reserved.
3  *
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>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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
17  *    distribution.
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.
21  *
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
33  * SUCH DAMAGE.
34  */
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/types.h>
39 #include <sys/uuid.h>
40 #include <sys/dirent.h>
41 
42 #include "hammer2.h"
43 
44 /*
45  * Return the directory entry type for an inode.
46  */
47 int
48 hammer2_get_dtype(uint8_t type)
49 {
50 	switch(type) {
51 	case HAMMER2_OBJTYPE_UNKNOWN:
52 		return (DT_UNKNOWN);
53 	case HAMMER2_OBJTYPE_DIRECTORY:
54 		return (DT_DIR);
55 	case HAMMER2_OBJTYPE_REGFILE:
56 		return (DT_REG);
57 	case HAMMER2_OBJTYPE_FIFO:
58 		return (DT_FIFO);
59 	case HAMMER2_OBJTYPE_CDEV:
60 		return (DT_CHR);
61 	case HAMMER2_OBJTYPE_BDEV:
62 		return (DT_BLK);
63 	case HAMMER2_OBJTYPE_SOFTLINK:
64 		return (DT_LNK);
65 	case HAMMER2_OBJTYPE_SOCKET:
66 		return (DT_SOCK);
67 	case HAMMER2_OBJTYPE_WHITEOUT:	/* not supported */
68 		return (DT_UNKNOWN);
69 	default:
70 		return (DT_UNKNOWN);
71 	}
72 	/* not reached */
73 }
74 
75 /*
76  * Return the directory entry type for an inode
77  */
78 int
79 hammer2_get_vtype(uint8_t type)
80 {
81 	switch(type) {
82 	case HAMMER2_OBJTYPE_UNKNOWN:
83 		return (VBAD);
84 	case HAMMER2_OBJTYPE_DIRECTORY:
85 		return (VDIR);
86 	case HAMMER2_OBJTYPE_REGFILE:
87 		return (VREG);
88 	case HAMMER2_OBJTYPE_FIFO:
89 		return (VFIFO);
90 	case HAMMER2_OBJTYPE_CDEV:
91 		return (VCHR);
92 	case HAMMER2_OBJTYPE_BDEV:
93 		return (VBLK);
94 	case HAMMER2_OBJTYPE_SOFTLINK:
95 		return (VLNK);
96 	case HAMMER2_OBJTYPE_SOCKET:
97 		return (VSOCK);
98 	case HAMMER2_OBJTYPE_WHITEOUT:	/* not supported */
99 		return (VBAD);
100 	default:
101 		return (VBAD);
102 	}
103 	/* not reached */
104 }
105 
106 uint8_t
107 hammer2_get_obj_type(enum vtype vtype)
108 {
109 	switch(vtype) {
110 	case VDIR:
111 		return(HAMMER2_OBJTYPE_DIRECTORY);
112 	case VREG:
113 		return(HAMMER2_OBJTYPE_REGFILE);
114 	case VFIFO:
115 		return(HAMMER2_OBJTYPE_FIFO);
116 	case VSOCK:
117 		return(HAMMER2_OBJTYPE_SOCKET);
118 	case VCHR:
119 		return(HAMMER2_OBJTYPE_CDEV);
120 	case VBLK:
121 		return(HAMMER2_OBJTYPE_BDEV);
122 	case VLNK:
123 		return(HAMMER2_OBJTYPE_SOFTLINK);
124 	default:
125 		return(HAMMER2_OBJTYPE_UNKNOWN);
126 	}
127 	/* not reached */
128 }
129 
130 /*
131  * Convert a hammer2 64-bit time to a timespec.
132  */
133 void
134 hammer2_time_to_timespec(uint64_t xtime, struct timespec *ts)
135 {
136 	ts->tv_sec = (unsigned long)(xtime / 1000000);
137 	ts->tv_nsec = (unsigned int)(xtime % 1000000) * 1000L;
138 }
139 
140 uint64_t
141 hammer2_timespec_to_time(const struct timespec *ts)
142 {
143 	uint64_t xtime;
144 
145 	xtime = (unsigned)(ts->tv_nsec / 1000) +
146 		(unsigned long)ts->tv_sec * 1000000ULL;
147 	return(xtime);
148 }
149 
150 /*
151  * Convert a uuid to a unix uid or gid
152  */
153 uint32_t
154 hammer2_to_unix_xid(const uuid_t *uuid)
155 {
156 	return(*(const uint32_t *)&uuid->node[2]);
157 }
158 
159 void
160 hammer2_guid_to_uuid(uuid_t *uuid, uint32_t guid)
161 {
162 	bzero(uuid, sizeof(*uuid));
163 	*(uint32_t *)&uuid->node[2] = guid;
164 }
165 
166 /*
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
169  * added together.
170  *
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).
174  *
175  * Also, the iscsi crc code is used instead of the old crc32 code.
176  */
177 hammer2_key_t
178 hammer2_dirhash(const unsigned char *name, size_t len)
179 {
180 	const unsigned char *aname = name;
181 	uint32_t crcx;
182 	uint64_t key;
183 	size_t i;
184 	size_t j;
185 
186 	key = 0;
187 
188 	/*
189 	 * m32
190 	 */
191 	crcx = 0;
192 	for (i = j = 0; i < len; ++i) {
193 		if (aname[i] == '.' ||
194 		    aname[i] == '-' ||
195 		    aname[i] == '_' ||
196 		    aname[i] == '~') {
197 			if (i != j)
198 				crcx += hammer2_icrc32(aname + j, i - j);
199 			j = i + 1;
200 		}
201 	}
202 	if (i != j)
203 		crcx += hammer2_icrc32(aname + j, i - j);
204 
205 	/*
206 	 * The directory hash utilizes the top 32 bits of the 64-bit key.
207 	 * Bit 63 must be set to 1.
208 	 */
209 	crcx |= 0x80000000U;
210 	key |= (uint64_t)crcx << 32;
211 
212 	/*
213 	 * l16 - crc of entire filename
214 	 *
215 	 * This crc reduces degenerate hash collision conditions
216 	 */
217 	crcx = hammer2_icrc32(aname, len);
218 	crcx = crcx ^ (crcx << 16);
219 	key |= crcx & 0xFFFF0000U;
220 
221 	/*
222 	 * Set bit 15.  This allows readdir to strip bit 63 so a positive
223 	 * 64-bit cookie/offset can always be returned, and still guarantee
224 	 * that the values 0x0000-0x7FFF are available for artificial entries.
225 	 * ('.' and '..').
226 	 */
227 	key |= 0x8000U;
228 
229 	return (key);
230 }
231 
232 /*
233  * Convert bytes to radix with no limitations.
234  *
235  * 0 bytes is special-cased to a radix of zero (which would normally
236  * translate to (1 << 0) == 1).
237  */
238 int
239 hammer2_getradix(size_t bytes)
240 {
241 	int radix;
242 
243 	/*
244 	 * Optimize the iteration by pre-checking commonly used radii.
245 	 */
246 	if (bytes == HAMMER2_PBUFSIZE)
247 		radix = HAMMER2_PBUFRADIX;
248 	else if (bytes >= HAMMER2_LBUFSIZE)
249 		radix = HAMMER2_LBUFRADIX;
250 	else if (bytes >= HAMMER2_ALLOC_MIN)	/* clamp */
251 		radix = HAMMER2_RADIX_MIN;
252 	else
253 		radix = 0;
254 
255 	/*
256 	 * Iterate as needed.  Note that bytes == 0 is expected to return
257 	 * a radix of 0 as a special case.
258 	 */
259 	while (((size_t)1 << radix) < bytes)
260 		++radix;
261 	return (radix);
262 }
263 
264 /*
265  * The logical block size is currently always PBUFSIZE.
266  */
267 int
268 hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
269 		     hammer2_key_t *lbasep, hammer2_key_t *leofp)
270 {
271 	KKASSERT(ip->flags & HAMMER2_INODE_METAGOOD);
272 	if (lbasep)
273 		*lbasep = uoff & ~HAMMER2_PBUFMASK64;
274 	if (leofp) {
275 		*leofp = (ip->meta.size + HAMMER2_PBUFMASK64) &
276 			 ~HAMMER2_PBUFMASK64;
277 	}
278 	return (HAMMER2_PBUFSIZE);
279 }
280 
281 /*
282  * Calculate the physical block size.  pblksize <= lblksize.  Primarily
283  * used to calculate a smaller physical block for the logical block
284  * containing the file EOF.
285  *
286  * Returns 0 if the requested base offset is beyond the file EOF.
287  */
288 int
289 hammer2_calc_physical(hammer2_inode_t *ip, hammer2_key_t lbase)
290 {
291 	int lblksize;
292 	int pblksize;
293 	int eofbytes;
294 
295 	KKASSERT(ip->flags & HAMMER2_INODE_METAGOOD);
296 	lblksize = hammer2_calc_logical(ip, lbase, NULL, NULL);
297 	if (lbase + lblksize <= ip->meta.size)
298 		return (lblksize);
299 	if (lbase >= ip->meta.size)
300 		return (0);
301 	eofbytes = (int)(ip->meta.size - lbase);
302 	pblksize = lblksize;
303 	while (pblksize >= eofbytes && pblksize >= HAMMER2_ALLOC_MIN)
304 		pblksize >>= 1;
305 	pblksize <<= 1;
306 
307 	return (pblksize);
308 }
309 
310 void
311 hammer2_update_time(uint64_t *timep)
312 {
313 	struct timespec ts;
314 
315 	vfs_timestamp(&ts);
316 	*timep = (unsigned long)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
317 }
318 
319 void
320 hammer2_adjreadcounter(int btype, size_t bytes)
321 {
322 	long *counterp;
323 
324 	switch(btype) {
325 	case HAMMER2_BREF_TYPE_DATA:
326 		counterp = &hammer2_iod_file_read;
327 		break;
328 	case HAMMER2_BREF_TYPE_DIRENT:
329 	case HAMMER2_BREF_TYPE_INODE:
330 		counterp = &hammer2_iod_meta_read;
331 		break;
332 	case HAMMER2_BREF_TYPE_INDIRECT:
333 		counterp = &hammer2_iod_indr_read;
334 		break;
335 	case HAMMER2_BREF_TYPE_FREEMAP_NODE:
336 	case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
337 		counterp = &hammer2_iod_fmap_read;
338 		break;
339 	case HAMMER2_BREF_TYPE_FREEMAP:
340 	case HAMMER2_BREF_TYPE_VOLUME:
341 		counterp = &hammer2_iod_volu_read;
342 		break;
343 	case HAMMER2_BREF_TYPE_EMPTY:
344 	default:
345 		return;
346 	}
347 	*counterp += bytes;
348 }
349 
350 void
351 hammer2_adjwritecounter(int btype, size_t bytes)
352 {
353 	long *counterp;
354 
355 	switch(btype) {
356 	case HAMMER2_BREF_TYPE_DATA:
357 		counterp = &hammer2_iod_file_write;
358 		break;
359 	case HAMMER2_BREF_TYPE_DIRENT:
360 	case HAMMER2_BREF_TYPE_INODE:
361 		counterp = &hammer2_iod_meta_write;
362 		break;
363 	case HAMMER2_BREF_TYPE_INDIRECT:
364 		counterp = &hammer2_iod_indr_write;
365 		break;
366 	case HAMMER2_BREF_TYPE_FREEMAP_NODE:
367 	case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
368 		counterp = &hammer2_iod_fmap_write;
369 		break;
370 	case HAMMER2_BREF_TYPE_FREEMAP:
371 	case HAMMER2_BREF_TYPE_VOLUME:
372 		counterp = &hammer2_iod_volu_write;
373 		break;
374 	case HAMMER2_BREF_TYPE_EMPTY:
375 	default:
376 		return;
377 	}
378 	*counterp += bytes;
379 }
380 
381 /*
382  * Check for pending signal to allow interruption.  This function will
383  * return immediately if the calling thread is a kernel thread and not
384  * a user thread.
385  */
386 int
387 hammer2_signal_check(time_t *timep)
388 {
389 	thread_t td = curthread;
390 	int error = 0;
391 
392 	if (td->td_lwp) {
393 		lwkt_user_yield();
394 		if (*timep != time_second) {
395 			*timep = time_second;
396 			if (CURSIG_NOBLOCK(curthread->td_lwp) != 0)
397 				error = HAMMER2_ERROR_ABORTED;
398 		}
399 	} else {
400 		lwkt_yield();
401 	}
402 	return error;
403 }
404 
405 const char *
406 hammer2_error_str(int error)
407 {
408 	if (error & HAMMER2_ERROR_EIO)
409 		return("I/O Error");
410 	if (error & HAMMER2_ERROR_CHECK)
411 		return("Check Error");
412 	if (error & HAMMER2_ERROR_INCOMPLETE)
413 		return("Cluster Quorum Error");
414 	if (error & HAMMER2_ERROR_DEPTH)
415 		return("Chain Depth Error");
416 	if (error & HAMMER2_ERROR_BADBREF)
417 		return("Bad Blockref Error");
418 	if (error & HAMMER2_ERROR_ENOSPC)
419 		return("No Space on Device");
420 	if (error & HAMMER2_ERROR_ENOENT)
421 		return("Entry Not Found");
422 	if (error & HAMMER2_ERROR_ENOTEMPTY)
423 		return("Directory Not Empty");
424 	if (error & HAMMER2_ERROR_EAGAIN)
425 		return("EAGAIN");
426 	if (error & HAMMER2_ERROR_ENOTDIR)
427 		return("Not a Directory");
428 	if (error & HAMMER2_ERROR_EISDIR)
429 		return("Is a Directory");
430 	if (error & HAMMER2_ERROR_EINPROGRESS)
431 		return("Operation in Progress");
432 	if (error & HAMMER2_ERROR_ABORTED)
433 		return("Operation Aborted");
434 	if (error & HAMMER2_ERROR_EOF)
435 		return("Operation Complete");
436 	if (error & HAMMER2_ERROR_EINVAL)
437 		return("Invalid Operation");
438 	if (error & HAMMER2_ERROR_EEXIST)
439 		return("Object Exists");
440 	if (error & HAMMER2_ERROR_EDEADLK)
441 		return("Deadlock Detected");
442 	if (error & HAMMER2_ERROR_ESRCH)
443 		return("Object Not Found");
444 	if (error & HAMMER2_ERROR_ETIMEDOUT)
445 		return("Timeout");
446 	return("Unknown Error");
447 }
448 
449 const char *
450 hammer2_bref_type_str(int btype)
451 {
452 	switch(btype) {
453 	case HAMMER2_BREF_TYPE_EMPTY:
454 		return("empty");
455 	case HAMMER2_BREF_TYPE_INODE:
456 		return("inode");
457 	case HAMMER2_BREF_TYPE_INDIRECT:
458 		return("indirect");
459 	case HAMMER2_BREF_TYPE_DATA:
460 		return("data");
461 	case HAMMER2_BREF_TYPE_DIRENT:
462 		return("dirent");
463 	case HAMMER2_BREF_TYPE_FREEMAP_NODE:
464 		return("freemap_node");
465 	case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
466 		return("freemap_leaf");
467 	case HAMMER2_BREF_TYPE_INVALID:
468 		return("invalid");
469 	case HAMMER2_BREF_TYPE_FREEMAP:
470 		return("freemap");
471 	case HAMMER2_BREF_TYPE_VOLUME:
472 		return("volume");
473 	default:
474 		return("unknown");
475 	}
476 }
477