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