xref: /src/sys/contrib/openzfs/lib/libzpool/kernel.c (revision 8a62a2a5659d1839d8799b4274c04469d7f17c78)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25  * Copyright (c) 2016 Actifio, Inc. All rights reserved.
26  * Copyright (c) 2025, Klara, Inc.
27  */
28 
29 #include <assert.h>
30 #include <fcntl.h>
31 #include <libgen.h>
32 #include <poll.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <limits.h>
37 #include <libzutil.h>
38 #include <sys/crypto/icp.h>
39 #include <sys/processor.h>
40 #include <sys/rrwlock.h>
41 #include <sys/spa.h>
42 #include <sys/spa_impl.h>
43 #include <sys/sid.h>
44 #include <sys/stat.h>
45 #include <sys/systeminfo.h>
46 #include <sys/time.h>
47 #include <sys/tsd.h>
48 
49 #include <libspl.h>
50 #include <libzpool.h>
51 #include <sys/zfs_context.h>
52 #include <sys/zfs_onexit.h>
53 #include <sys/zfs_vfsops.h>
54 #include <sys/zstd/zstd.h>
55 #include <sys/zvol.h>
56 #include <zfs_fletcher.h>
57 #include <zlib.h>
58 
59 /*
60  * Emulation of kernel services in userland.
61  */
62 
63 uint32_t hostid;
64 
65 /* If set, all blocks read will be copied to the specified directory. */
66 char *vn_dumpdir = NULL;
67 
68 uint32_t
zone_get_hostid(void * zonep)69 zone_get_hostid(void *zonep)
70 {
71 	/*
72 	 * We're emulating the system's hostid in userland.
73 	 */
74 	(void) zonep;
75 	return (hostid);
76 }
77 
78 /*
79  * =========================================================================
80  * vnode operations
81  * =========================================================================
82  */
83 
84 /*
85  * =========================================================================
86  * Figure out which debugging statements to print
87  * =========================================================================
88  */
89 
90 static char *dprintf_string;
91 static int dprintf_print_all;
92 
93 int
dprintf_find_string(const char * string)94 dprintf_find_string(const char *string)
95 {
96 	char *tmp_str = dprintf_string;
97 	int len = strlen(string);
98 
99 	/*
100 	 * Find out if this is a string we want to print.
101 	 * String format: file1.c,function_name1,file2.c,file3.c
102 	 */
103 
104 	while (tmp_str != NULL) {
105 		if (strncmp(tmp_str, string, len) == 0 &&
106 		    (tmp_str[len] == ',' || tmp_str[len] == '\0'))
107 			return (1);
108 		tmp_str = strchr(tmp_str, ',');
109 		if (tmp_str != NULL)
110 			tmp_str++; /* Get rid of , */
111 	}
112 	return (0);
113 }
114 
115 void
dprintf_setup(int * argc,char ** argv)116 dprintf_setup(int *argc, char **argv)
117 {
118 	int i, j;
119 
120 	/*
121 	 * Debugging can be specified two ways: by setting the
122 	 * environment variable ZFS_DEBUG, or by including a
123 	 * "debug=..."  argument on the command line.  The command
124 	 * line setting overrides the environment variable.
125 	 */
126 
127 	for (i = 1; i < *argc; i++) {
128 		int len = strlen("debug=");
129 		/* First look for a command line argument */
130 		if (strncmp("debug=", argv[i], len) == 0) {
131 			dprintf_string = argv[i] + len;
132 			/* Remove from args */
133 			for (j = i; j < *argc; j++)
134 				argv[j] = argv[j+1];
135 			argv[j] = NULL;
136 			(*argc)--;
137 		}
138 	}
139 
140 	if (dprintf_string == NULL) {
141 		/* Look for ZFS_DEBUG environment variable */
142 		dprintf_string = getenv("ZFS_DEBUG");
143 	}
144 
145 	/*
146 	 * Are we just turning on all debugging?
147 	 */
148 	if (dprintf_find_string("on"))
149 		dprintf_print_all = 1;
150 
151 	if (dprintf_string != NULL)
152 		zfs_flags |= ZFS_DEBUG_DPRINTF;
153 }
154 
155 /*
156  * =========================================================================
157  * debug printfs
158  * =========================================================================
159  */
160 void
__dprintf(boolean_t dprint,const char * file,const char * func,int line,const char * fmt,...)161 __dprintf(boolean_t dprint, const char *file, const char *func,
162     int line, const char *fmt, ...)
163 {
164 	/* Get rid of annoying "../common/" prefix to filename. */
165 	const char *newfile = zfs_basename(file);
166 
167 	va_list adx;
168 	if (dprint) {
169 		/* dprintf messages are printed immediately */
170 
171 		if (!dprintf_print_all &&
172 		    !dprintf_find_string(newfile) &&
173 		    !dprintf_find_string(func))
174 			return;
175 
176 		/* Print out just the function name if requested */
177 		flockfile(stdout);
178 		if (dprintf_find_string("pid"))
179 			(void) printf("%d ", getpid());
180 		if (dprintf_find_string("tid"))
181 			(void) printf("%ju ",
182 			    (uintmax_t)(uintptr_t)pthread_self());
183 		if (dprintf_find_string("cpu"))
184 			(void) printf("%u ", getcpuid());
185 		if (dprintf_find_string("time"))
186 			(void) printf("%llu ", gethrtime());
187 		if (dprintf_find_string("long"))
188 			(void) printf("%s, line %d: ", newfile, line);
189 		(void) printf("dprintf: %s: ", func);
190 		va_start(adx, fmt);
191 		(void) vprintf(fmt, adx);
192 		va_end(adx);
193 		funlockfile(stdout);
194 	} else {
195 		/* zfs_dbgmsg is logged for dumping later */
196 		size_t size;
197 		char *buf;
198 		int i;
199 
200 		size = 1024;
201 		buf = umem_alloc(size, UMEM_NOFAIL);
202 		i = snprintf(buf, size, "%s:%d:%s(): ", newfile, line, func);
203 
204 		if (i < size) {
205 			va_start(adx, fmt);
206 			(void) vsnprintf(buf + i, size - i, fmt, adx);
207 			va_end(adx);
208 		}
209 
210 		__zfs_dbgmsg(buf);
211 
212 		umem_free(buf, size);
213 	}
214 }
215 
216 /*
217  * =========================================================================
218  * cmn_err() and panic()
219  * =========================================================================
220  */
221 
222 static __attribute__((noreturn)) void
panic_stop_or_abort(void)223 panic_stop_or_abort(void)
224 {
225 	const char *stopenv = getenv("LIBZPOOL_PANIC_STOP");
226 	if (stopenv != NULL && atoi(stopenv)) {
227 		fputs("libzpool: LIBZPOOL_PANIC_STOP is set, sending "
228 		    "SIGSTOP to process group\n", stderr);
229 		fflush(stderr);
230 
231 		kill(0, SIGSTOP);
232 
233 		fputs("libzpool: continued after panic stop, "
234 		    "aborting\n", stderr);
235 	}
236 
237 	abort();	/* think of it as a "user-level crash dump" */
238 }
239 
240 static void
vcmn_msg(int ce,const char * fmt,va_list adx)241 vcmn_msg(int ce, const char *fmt, va_list adx)
242 {
243 	switch (ce) {
244 	case CE_IGNORE:
245 		return;
246 	case CE_CONT:
247 		break;
248 	case CE_NOTE:
249 		fputs("libzpool: NOTICE: ", stderr);
250 		break;
251 	case CE_WARN:
252 		fputs("libzpool: WARNING: ", stderr);
253 		break;
254 	case CE_PANIC:
255 		fputs("libzpool: PANIC: ", stderr);
256 		break;
257 	default:
258 		fputs("libzpool: [unknown severity %d]: ", stderr);
259 		break;
260 	}
261 
262 	vfprintf(stderr, fmt, adx);
263 	if (ce != CE_CONT)
264 		fputc('\n', stderr);
265 	fflush(stderr);
266 }
267 
268 void
vcmn_err(int ce,const char * fmt,va_list adx)269 vcmn_err(int ce, const char *fmt, va_list adx)
270 {
271 	vcmn_msg(ce, fmt, adx);
272 
273 	if (ce == CE_PANIC)
274 		panic_stop_or_abort();
275 }
276 
277 void
cmn_err(int ce,const char * fmt,...)278 cmn_err(int ce, const char *fmt, ...)
279 {
280 	va_list adx;
281 
282 	va_start(adx, fmt);
283 	vcmn_err(ce, fmt, adx);
284 	va_end(adx);
285 }
286 
287 __attribute__((noreturn)) void
panic(const char * fmt,...)288 panic(const char *fmt, ...)
289 {
290 	va_list adx;
291 
292 	va_start(adx, fmt);
293 	vcmn_msg(CE_PANIC, fmt, adx);
294 	va_end(adx);
295 
296 	panic_stop_or_abort();
297 }
298 
299 __attribute__((noreturn)) void
vpanic(const char * fmt,va_list adx)300 vpanic(const char *fmt, va_list adx)
301 {
302 	vcmn_msg(CE_PANIC, fmt, adx);
303 	panic_stop_or_abort();
304 }
305 
306 /*
307  * =========================================================================
308  * misc routines
309  * =========================================================================
310  */
311 
312 void
delay(clock_t ticks)313 delay(clock_t ticks)
314 {
315 	(void) poll(0, 0, ticks * (1000 / hz));
316 }
317 
318 int
ddi_strtoull(const char * str,char ** nptr,int base,u_longlong_t * result)319 ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
320 {
321 	errno = 0;
322 	*result = strtoull(str, nptr, base);
323 	if (*result == 0)
324 		return (errno);
325 	return (0);
326 }
327 
328 /*
329  * =========================================================================
330  * kernel emulation setup & teardown
331  * =========================================================================
332  */
333 static int
umem_out_of_memory(void)334 umem_out_of_memory(void)
335 {
336 	char errmsg[] = "out of memory -- generating core dump\n";
337 
338 	(void) fprintf(stderr, "%s", errmsg);
339 	abort();
340 	return (0);
341 }
342 
343 static void
spa_config_load(void)344 spa_config_load(void)
345 {
346 	void *buf = NULL;
347 	nvlist_t *nvlist, *child;
348 	nvpair_t *nvpair;
349 	char *pathname;
350 	zfs_file_t *fp;
351 	zfs_file_attr_t zfa;
352 	uint64_t fsize;
353 	int err;
354 
355 	/*
356 	 * Open the configuration file.
357 	 */
358 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
359 
360 	(void) snprintf(pathname, MAXPATHLEN, "%s", spa_config_path);
361 
362 	err = zfs_file_open(pathname, O_RDONLY, 0, &fp);
363 	if (err)
364 		err = zfs_file_open(ZPOOL_CACHE_BOOT, O_RDONLY, 0, &fp);
365 
366 	kmem_free(pathname, MAXPATHLEN);
367 
368 	if (err)
369 		return;
370 
371 	if (zfs_file_getattr(fp, &zfa))
372 		goto out;
373 
374 	fsize = zfa.zfa_size;
375 	buf = kmem_alloc(fsize, KM_SLEEP);
376 
377 	/*
378 	 * Read the nvlist from the file.
379 	 */
380 	if (zfs_file_read(fp, buf, fsize, NULL) < 0)
381 		goto out;
382 
383 	/*
384 	 * Unpack the nvlist.
385 	 */
386 	if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
387 		goto out;
388 
389 	/*
390 	 * Iterate over all elements in the nvlist, creating a new spa_t for
391 	 * each one with the specified configuration.
392 	 */
393 	spa_namespace_enter(FTAG);
394 	nvpair = NULL;
395 	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
396 		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
397 			continue;
398 
399 		child = fnvpair_value_nvlist(nvpair);
400 
401 		if (spa_lookup(nvpair_name(nvpair)) != NULL)
402 			continue;
403 		(void) spa_add(nvpair_name(nvpair), child, NULL);
404 	}
405 	spa_namespace_exit(FTAG);
406 
407 	nvlist_free(nvlist);
408 
409 out:
410 	if (buf != NULL)
411 		kmem_free(buf, fsize);
412 
413 	zfs_file_close(fp);
414 }
415 
416 void
kernel_init(int mode)417 kernel_init(int mode)
418 {
419 	extern uint_t rrw_tsd_key;
420 
421 	libspl_init();
422 
423 	umem_nofail_callback(umem_out_of_memory);
424 
425 	dprintf("physmem = %llu pages (%.2f GB)\n", (u_longlong_t)physmem,
426 	    (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
427 
428 	hostid = (mode & SPA_MODE_WRITE) ? get_system_hostid() : 0;
429 
430 	system_taskq_init();
431 	icp_init();
432 
433 	zstd_init();
434 
435 	spa_init((spa_mode_t)mode);
436 	spa_config_load();
437 
438 	fletcher_4_init();
439 
440 	tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
441 }
442 
443 void
kernel_fini(void)444 kernel_fini(void)
445 {
446 	fletcher_4_fini();
447 	spa_fini();
448 
449 	zstd_fini();
450 
451 	icp_fini();
452 	system_taskq_fini();
453 
454 	libspl_fini();
455 }
456 
457 zfs_file_t *
zfs_onexit_fd_hold(int fd,minor_t * minorp)458 zfs_onexit_fd_hold(int fd, minor_t *minorp)
459 {
460 	(void) fd;
461 	*minorp = 0;
462 	return (NULL);
463 }
464 
465 void
zfs_onexit_fd_rele(zfs_file_t * fp)466 zfs_onexit_fd_rele(zfs_file_t *fp)
467 {
468 	(void) fp;
469 }
470 
471 int
zfs_onexit_add_cb(minor_t minor,void (* func)(void *),void * data,uintptr_t * action_handle)472 zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
473     uintptr_t *action_handle)
474 {
475 	(void) minor, (void) func, (void) data, (void) action_handle;
476 	return (0);
477 }
478 
479 void
zvol_create_minors(const char * name)480 zvol_create_minors(const char *name)
481 {
482 	(void) name;
483 }
484 
485 void
zvol_remove_minors(spa_t * spa,const char * name,boolean_t async)486 zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
487 {
488 	(void) spa, (void) name, (void) async;
489 }
490 
491 void
zvol_rename_minors(spa_t * spa,const char * oldname,const char * newname,boolean_t async)492 zvol_rename_minors(spa_t *spa, const char *oldname, const char *newname,
493     boolean_t async)
494 {
495 	(void) spa, (void) oldname, (void) newname, (void) async;
496 }
497 
498 /*
499  * Open file
500  *
501  * path - fully qualified path to file
502  * flags - file attributes O_READ / O_WRITE / O_EXCL
503  * fpp - pointer to return file pointer
504  *
505  * Returns 0 on success underlying error on failure.
506  */
507 int
zfs_file_open(const char * path,int flags,int mode,zfs_file_t ** fpp)508 zfs_file_open(const char *path, int flags, int mode, zfs_file_t **fpp)
509 {
510 	int fd;
511 	int dump_fd;
512 	int err;
513 	int old_umask = 0;
514 	zfs_file_t *fp;
515 	struct stat64 st;
516 
517 	if (!(flags & O_CREAT) && stat64(path, &st) == -1)
518 		return (errno);
519 
520 	if (!(flags & O_CREAT) && S_ISBLK(st.st_mode))
521 		flags |= O_DIRECT;
522 
523 	if (flags & O_CREAT)
524 		old_umask = umask(0);
525 
526 	fd = open64(path, flags, mode);
527 	if (fd == -1)
528 		return (errno);
529 
530 	if (flags & O_CREAT)
531 		(void) umask(old_umask);
532 
533 	if (vn_dumpdir != NULL) {
534 		char *dumppath = umem_zalloc(MAXPATHLEN, UMEM_NOFAIL);
535 		const char *inpath = zfs_basename(path);
536 
537 		(void) snprintf(dumppath, MAXPATHLEN,
538 		    "%s/%s", vn_dumpdir, inpath);
539 		dump_fd = open64(dumppath, O_CREAT | O_WRONLY, 0666);
540 		umem_free(dumppath, MAXPATHLEN);
541 		if (dump_fd == -1) {
542 			err = errno;
543 			close(fd);
544 			return (err);
545 		}
546 	} else {
547 		dump_fd = -1;
548 	}
549 
550 	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
551 
552 	fp = umem_zalloc(sizeof (zfs_file_t), UMEM_NOFAIL);
553 	fp->f_fd = fd;
554 	fp->f_dump_fd = dump_fd;
555 	*fpp = fp;
556 
557 	return (0);
558 }
559 
560 void
zfs_file_close(zfs_file_t * fp)561 zfs_file_close(zfs_file_t *fp)
562 {
563 	close(fp->f_fd);
564 	if (fp->f_dump_fd != -1)
565 		close(fp->f_dump_fd);
566 
567 	umem_free(fp, sizeof (zfs_file_t));
568 }
569 
570 /*
571  * Stateful write - use os internal file pointer to determine where to
572  * write and update on successful completion.
573  *
574  * fp -  pointer to file (pipe, socket, etc) to write to
575  * buf - buffer to write
576  * count - # of bytes to write
577  * resid -  pointer to count of unwritten bytes  (if short write)
578  *
579  * Returns 0 on success errno on failure.
580  */
581 int
zfs_file_write(zfs_file_t * fp,const void * buf,size_t count,ssize_t * resid)582 zfs_file_write(zfs_file_t *fp, const void *buf, size_t count, ssize_t *resid)
583 {
584 	ssize_t rc;
585 
586 	rc = write(fp->f_fd, buf, count);
587 	if (rc < 0)
588 		return (errno);
589 
590 	if (resid) {
591 		*resid = count - rc;
592 	} else if (rc != count) {
593 		return (EIO);
594 	}
595 
596 	return (0);
597 }
598 
599 /*
600  * Stateless write - os internal file pointer is not updated.
601  *
602  * fp -  pointer to file (pipe, socket, etc) to write to
603  * buf - buffer to write
604  * count - # of bytes to write
605  * off - file offset to write to (only valid for seekable types)
606  * resid -  pointer to count of unwritten bytes
607  *
608  * Returns 0 on success errno on failure.
609  */
610 int
zfs_file_pwrite(zfs_file_t * fp,const void * buf,size_t count,loff_t pos,uint8_t ashift,ssize_t * resid)611 zfs_file_pwrite(zfs_file_t *fp, const void *buf,
612     size_t count, loff_t pos, uint8_t ashift, ssize_t *resid)
613 {
614 	ssize_t rc, split, done;
615 	int sectors;
616 
617 	/*
618 	 * To simulate partial disk writes, we split writes into two
619 	 * system calls so that the process can be killed in between.
620 	 * This is used by ztest to simulate realistic failure modes.
621 	 */
622 	sectors = count >> ashift;
623 	split = (sectors > 0 ? rand() % sectors : 0) << ashift;
624 	rc = pwrite64(fp->f_fd, buf, split, pos);
625 	if (rc != -1) {
626 		done = rc;
627 		rc = pwrite64(fp->f_fd, (char *)buf + split,
628 		    count - split, pos + split);
629 	}
630 #ifdef __linux__
631 	if (rc == -1 && errno == EINVAL) {
632 		/*
633 		 * Under Linux, this most likely means an alignment issue
634 		 * (memory or disk) due to O_DIRECT, so we abort() in order
635 		 * to catch the offender.
636 		 */
637 		abort();
638 	}
639 #endif
640 
641 	if (rc < 0)
642 		return (errno);
643 
644 	done += rc;
645 
646 	if (resid) {
647 		*resid = count - done;
648 	} else if (done != count) {
649 		return (EIO);
650 	}
651 
652 	return (0);
653 }
654 
655 /*
656  * Stateful read - use os internal file pointer to determine where to
657  * read and update on successful completion.
658  *
659  * fp -  pointer to file (pipe, socket, etc) to read from
660  * buf - buffer to write
661  * count - # of bytes to read
662  * resid -  pointer to count of unread bytes (if short read)
663  *
664  * Returns 0 on success errno on failure.
665  */
666 int
zfs_file_read(zfs_file_t * fp,void * buf,size_t count,ssize_t * resid)667 zfs_file_read(zfs_file_t *fp, void *buf, size_t count, ssize_t *resid)
668 {
669 	int rc;
670 
671 	rc = read(fp->f_fd, buf, count);
672 	if (rc < 0)
673 		return (errno);
674 
675 	if (resid) {
676 		*resid = count - rc;
677 	} else if (rc != count) {
678 		return (EIO);
679 	}
680 
681 	return (0);
682 }
683 
684 /*
685  * Stateless read - os internal file pointer is not updated.
686  *
687  * fp -  pointer to file (pipe, socket, etc) to read from
688  * buf - buffer to write
689  * count - # of bytes to write
690  * off - file offset to read from (only valid for seekable types)
691  * resid -  pointer to count of unwritten bytes (if short write)
692  *
693  * Returns 0 on success errno on failure.
694  */
695 int
zfs_file_pread(zfs_file_t * fp,void * buf,size_t count,loff_t off,ssize_t * resid)696 zfs_file_pread(zfs_file_t *fp, void *buf, size_t count, loff_t off,
697     ssize_t *resid)
698 {
699 	ssize_t rc;
700 
701 	rc = pread64(fp->f_fd, buf, count, off);
702 	if (rc < 0) {
703 #ifdef __linux__
704 		/*
705 		 * Under Linux, this most likely means an alignment issue
706 		 * (memory or disk) due to O_DIRECT, so we abort() in order to
707 		 * catch the offender.
708 		 */
709 		if (errno == EINVAL)
710 			abort();
711 #endif
712 		return (errno);
713 	}
714 
715 	if (fp->f_dump_fd != -1) {
716 		int status;
717 
718 		status = pwrite64(fp->f_dump_fd, buf, rc, off);
719 		ASSERT(status != -1);
720 	}
721 
722 	if (resid) {
723 		*resid = count - rc;
724 	} else if (rc != count) {
725 		return (EIO);
726 	}
727 
728 	return (0);
729 }
730 
731 /*
732  * lseek - set / get file pointer
733  *
734  * fp -  pointer to file (pipe, socket, etc) to read from
735  * offp - value to seek to, returns current value plus passed offset
736  * whence - see man pages for standard lseek whence values
737  *
738  * Returns 0 on success errno on failure (ESPIPE for non seekable types)
739  */
740 int
zfs_file_seek(zfs_file_t * fp,loff_t * offp,int whence)741 zfs_file_seek(zfs_file_t *fp, loff_t *offp, int whence)
742 {
743 	loff_t rc;
744 
745 	rc = lseek(fp->f_fd, *offp, whence);
746 	if (rc < 0)
747 		return (errno);
748 
749 	*offp = rc;
750 
751 	return (0);
752 }
753 
754 /*
755  * Get file attributes
756  *
757  * filp - file pointer
758  * zfattr - pointer to file attr structure
759  *
760  * Currently only used for fetching size and file mode
761  *
762  * Returns 0 on success or error code of underlying getattr call on failure.
763  */
764 int
zfs_file_getattr(zfs_file_t * fp,zfs_file_attr_t * zfattr)765 zfs_file_getattr(zfs_file_t *fp, zfs_file_attr_t *zfattr)
766 {
767 	struct stat64 st;
768 
769 	if (fstat64_blk(fp->f_fd, &st) == -1)
770 		return (errno);
771 
772 	zfattr->zfa_size = st.st_size;
773 	zfattr->zfa_mode = st.st_mode;
774 
775 	return (0);
776 }
777 
778 /*
779  * Sync file to disk
780  *
781  * filp - file pointer
782  * flags - O_SYNC and or O_DSYNC
783  *
784  * Returns 0 on success or error code of underlying sync call on failure.
785  */
786 int
zfs_file_fsync(zfs_file_t * fp,int flags)787 zfs_file_fsync(zfs_file_t *fp, int flags)
788 {
789 	(void) flags;
790 
791 	if (fsync(fp->f_fd) < 0)
792 		return (errno);
793 
794 	return (0);
795 }
796 
797 /*
798  * deallocate - zero and/or deallocate file storage
799  *
800  * fp - file pointer
801  * offset - offset to start zeroing or deallocating
802  * len - length to zero or deallocate
803  */
804 int
zfs_file_deallocate(zfs_file_t * fp,loff_t offset,loff_t len)805 zfs_file_deallocate(zfs_file_t *fp, loff_t offset, loff_t len)
806 {
807 	int rc;
808 #if defined(__linux__)
809 	rc = fallocate(fp->f_fd,
810 	    FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, len);
811 #elif defined(__FreeBSD__) && (__FreeBSD_version >= 1400029)
812 	struct spacectl_range rqsr = {
813 		.r_offset = offset,
814 		.r_len = len,
815 	};
816 	rc = fspacectl(fp->f_fd, SPACECTL_DEALLOC, &rqsr, 0, &rqsr);
817 #else
818 	(void) fp, (void) offset, (void) len;
819 	rc = EOPNOTSUPP;
820 #endif
821 	if (rc)
822 		return (SET_ERROR(rc));
823 	return (0);
824 }
825 
826 /*
827  * Request current file pointer offset
828  *
829  * fp - pointer to file
830  *
831  * Returns current file offset.
832  */
833 loff_t
zfs_file_off(zfs_file_t * fp)834 zfs_file_off(zfs_file_t *fp)
835 {
836 	return (lseek(fp->f_fd, SEEK_CUR, 0));
837 }
838 
839 /*
840  * unlink file
841  *
842  * path - fully qualified file path
843  *
844  * Returns 0 on success.
845  *
846  * OPTIONAL
847  */
848 int
zfs_file_unlink(const char * path)849 zfs_file_unlink(const char *path)
850 {
851 	return (remove(path));
852 }
853 
854 /*
855  * Get reference to file pointer
856  *
857  * fd - input file descriptor
858  *
859  * Returns pointer to file struct or NULL.
860  * Unsupported in user space.
861  */
862 zfs_file_t *
zfs_file_get(int fd)863 zfs_file_get(int fd)
864 {
865 	(void) fd;
866 	abort();
867 	return (NULL);
868 }
869 /*
870  * Drop reference to file pointer
871  *
872  * fp - pointer to file struct
873  *
874  * Unsupported in user space.
875  */
876 void
zfs_file_put(zfs_file_t * fp)877 zfs_file_put(zfs_file_t *fp)
878 {
879 	abort();
880 	(void) fp;
881 }
882 
883 void
zfsvfs_update_fromname(const char * oldname,const char * newname)884 zfsvfs_update_fromname(const char *oldname, const char *newname)
885 {
886 	(void) oldname, (void) newname;
887 }
888 
889 void
spa_import_os(spa_t * spa)890 spa_import_os(spa_t *spa)
891 {
892 	(void) spa;
893 }
894 
895 void
spa_export_os(spa_t * spa)896 spa_export_os(spa_t *spa)
897 {
898 	(void) spa;
899 }
900 
901 void
spa_activate_os(spa_t * spa)902 spa_activate_os(spa_t *spa)
903 {
904 	(void) spa;
905 }
906 
907 void
spa_deactivate_os(spa_t * spa)908 spa_deactivate_os(spa_t *spa)
909 {
910 	(void) spa;
911 }
912