4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
24 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
34 #include <sys/crypto/icp.h>
35 #include <sys/processor.h>
36 #include <sys/rrwlock.h>
39 #include <sys/systeminfo.h>
41 #include <sys/utsname.h>
42 #include <sys/zfs_context.h>
43 #include <sys/zfs_onexit.h>
44 #include <sys/zfs_vfsops.h>
45 #include <sys/zstd/zstd.h>
47 #include <zfs_fletcher.h>
51 * Emulation of kernel services in userland.
55 char hw_serial[HW_HOSTID_LEN];
56 struct utsname hw_utsname;
58 /* If set, all blocks read will be copied to the specified directory. */
59 char *vn_dumpdir = NULL;
61 /* this only exists to have its address taken */
65 * =========================================================================
67 * =========================================================================
69 * TS_STACK_MIN is dictated by the minimum allowed pthread stack size. While
70 * TS_STACK_MAX is somewhat arbitrary, it was selected to be large enough for
71 * the expected stack depth while small enough to avoid exhausting address
72 * space with high thread counts.
74 #define TS_STACK_MIN MAX(PTHREAD_STACK_MIN, 32768)
75 #define TS_STACK_MAX (256 * 1024)
79 zk_thread_create(void (*func)(void *), void *arg, size_t stksize, int state)
84 int detachstate = PTHREAD_CREATE_DETACHED;
86 VERIFY0(pthread_attr_init(&attr));
88 if (state & TS_JOINABLE)
89 detachstate = PTHREAD_CREATE_JOINABLE;
91 VERIFY0(pthread_attr_setdetachstate(&attr, detachstate));
94 * We allow the default stack size in user space to be specified by
95 * setting the ZFS_STACK_SIZE environment variable. This allows us
96 * the convenience of observing and debugging stack overruns in
97 * user space. Explicitly specified stack sizes will be honored.
98 * The usage of ZFS_STACK_SIZE is discussed further in the
99 * ENVIRONMENT VARIABLES sections of the ztest(1) man page.
102 stkstr = getenv("ZFS_STACK_SIZE");
105 stksize = TS_STACK_MAX;
107 stksize = MAX(atoi(stkstr), TS_STACK_MIN);
110 VERIFY3S(stksize, >, 0);
111 stksize = P2ROUNDUP(MAX(stksize, TS_STACK_MIN), PAGESIZE);
114 * If this ever fails, it may be because the stack size is not a
115 * multiple of system page size.
117 VERIFY0(pthread_attr_setstacksize(&attr, stksize));
118 VERIFY0(pthread_attr_setguardsize(&attr, PAGESIZE));
120 VERIFY0(pthread_create(&tid, &attr, (void *(*)(void *))func, arg));
121 VERIFY0(pthread_attr_destroy(&attr));
123 return ((void *)(uintptr_t)tid);
127 * =========================================================================
129 * =========================================================================
133 kstat_create(const char *module, int instance, const char *name,
134 const char *class, uchar_t type, ulong_t ndata, uchar_t ks_flag)
141 kstat_install(kstat_t *ksp)
146 kstat_delete(kstat_t *ksp)
151 kstat_waitq_enter(kstat_io_t *kiop)
156 kstat_waitq_exit(kstat_io_t *kiop)
161 kstat_runq_enter(kstat_io_t *kiop)
166 kstat_runq_exit(kstat_io_t *kiop)
171 kstat_waitq_to_runq(kstat_io_t *kiop)
176 kstat_runq_back_to_waitq(kstat_io_t *kiop)
180 kstat_set_raw_ops(kstat_t *ksp,
181 int (*headers)(char *buf, size_t size),
182 int (*data)(char *buf, size_t size, void *data),
183 void *(*addr)(kstat_t *ksp, loff_t index))
187 * =========================================================================
189 * =========================================================================
193 mutex_init(kmutex_t *mp, char *name, int type, void *cookie)
195 VERIFY0(pthread_mutex_init(&mp->m_lock, NULL));
196 memset(&mp->m_owner, 0, sizeof (pthread_t));
200 mutex_destroy(kmutex_t *mp)
202 VERIFY0(pthread_mutex_destroy(&mp->m_lock));
206 mutex_enter(kmutex_t *mp)
208 VERIFY0(pthread_mutex_lock(&mp->m_lock));
209 mp->m_owner = pthread_self();
213 mutex_tryenter(kmutex_t *mp)
217 error = pthread_mutex_trylock(&mp->m_lock);
219 mp->m_owner = pthread_self();
222 VERIFY3S(error, ==, EBUSY);
228 mutex_exit(kmutex_t *mp)
230 memset(&mp->m_owner, 0, sizeof (pthread_t));
231 VERIFY0(pthread_mutex_unlock(&mp->m_lock));
235 * =========================================================================
237 * =========================================================================
241 rw_init(krwlock_t *rwlp, char *name, int type, void *arg)
243 VERIFY0(pthread_rwlock_init(&rwlp->rw_lock, NULL));
244 rwlp->rw_readers = 0;
249 rw_destroy(krwlock_t *rwlp)
251 VERIFY0(pthread_rwlock_destroy(&rwlp->rw_lock));
255 rw_enter(krwlock_t *rwlp, krw_t rw)
257 if (rw == RW_READER) {
258 VERIFY0(pthread_rwlock_rdlock(&rwlp->rw_lock));
259 atomic_inc_uint(&rwlp->rw_readers);
261 VERIFY0(pthread_rwlock_wrlock(&rwlp->rw_lock));
262 rwlp->rw_owner = pthread_self();
267 rw_exit(krwlock_t *rwlp)
269 if (RW_READ_HELD(rwlp))
270 atomic_dec_uint(&rwlp->rw_readers);
274 VERIFY0(pthread_rwlock_unlock(&rwlp->rw_lock));
278 rw_tryenter(krwlock_t *rwlp, krw_t rw)
283 error = pthread_rwlock_tryrdlock(&rwlp->rw_lock);
285 error = pthread_rwlock_trywrlock(&rwlp->rw_lock);
289 atomic_inc_uint(&rwlp->rw_readers);
291 rwlp->rw_owner = pthread_self();
296 VERIFY3S(error, ==, EBUSY);
303 zone_get_hostid(void *zonep)
306 * We're emulating the system's hostid in userland.
308 return (strtoul(hw_serial, NULL, 10));
312 rw_tryupgrade(krwlock_t *rwlp)
318 * =========================================================================
319 * condition variables
320 * =========================================================================
324 cv_init(kcondvar_t *cv, char *name, int type, void *arg)
326 VERIFY0(pthread_cond_init(cv, NULL));
330 cv_destroy(kcondvar_t *cv)
332 VERIFY0(pthread_cond_destroy(cv));
336 cv_wait(kcondvar_t *cv, kmutex_t *mp)
338 memset(&mp->m_owner, 0, sizeof (pthread_t));
339 VERIFY0(pthread_cond_wait(cv, &mp->m_lock));
340 mp->m_owner = pthread_self();
344 cv_wait_sig(kcondvar_t *cv, kmutex_t *mp)
351 cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
358 delta = abstime - ddi_get_lbolt();
362 VERIFY(gettimeofday(&tv, NULL) == 0);
364 ts.tv_sec = tv.tv_sec + delta / hz;
365 ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % hz) * (NANOSEC / hz);
366 if (ts.tv_nsec >= NANOSEC) {
368 ts.tv_nsec -= NANOSEC;
371 memset(&mp->m_owner, 0, sizeof (pthread_t));
372 error = pthread_cond_timedwait(cv, &mp->m_lock, &ts);
373 mp->m_owner = pthread_self();
375 if (error == ETIMEDOUT)
385 cv_timedwait_hires(kcondvar_t *cv, kmutex_t *mp, hrtime_t tim, hrtime_t res,
393 ASSERT(flag == 0 || flag == CALLOUT_FLAG_ABSOLUTE);
396 if (flag & CALLOUT_FLAG_ABSOLUTE)
397 delta -= gethrtime();
402 VERIFY0(gettimeofday(&tv, NULL));
404 ts.tv_sec = tv.tv_sec + delta / NANOSEC;
405 ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % NANOSEC);
406 if (ts.tv_nsec >= NANOSEC) {
408 ts.tv_nsec -= NANOSEC;
411 memset(&mp->m_owner, 0, sizeof (pthread_t));
412 error = pthread_cond_timedwait(cv, &mp->m_lock, &ts);
413 mp->m_owner = pthread_self();
415 if (error == ETIMEDOUT)
424 cv_signal(kcondvar_t *cv)
426 VERIFY0(pthread_cond_signal(cv));
430 cv_broadcast(kcondvar_t *cv)
432 VERIFY0(pthread_cond_broadcast(cv));
436 * =========================================================================
438 * =========================================================================
442 seq_printf(struct seq_file *m, const char *fmt, ...)
446 procfs_list_install(const char *module,
447 const char *submodule,
450 procfs_list_t *procfs_list,
451 int (*show)(struct seq_file *f, void *p),
452 int (*show_header)(struct seq_file *f),
453 int (*clear)(procfs_list_t *procfs_list),
454 size_t procfs_list_node_off)
456 mutex_init(&procfs_list->pl_lock, NULL, MUTEX_DEFAULT, NULL);
457 list_create(&procfs_list->pl_list,
458 procfs_list_node_off + sizeof (procfs_list_node_t),
459 procfs_list_node_off + offsetof(procfs_list_node_t, pln_link));
460 procfs_list->pl_next_id = 1;
461 procfs_list->pl_node_offset = procfs_list_node_off;
465 procfs_list_uninstall(procfs_list_t *procfs_list)
469 procfs_list_destroy(procfs_list_t *procfs_list)
471 ASSERT(list_is_empty(&procfs_list->pl_list));
472 list_destroy(&procfs_list->pl_list);
473 mutex_destroy(&procfs_list->pl_lock);
476 #define NODE_ID(procfs_list, obj) \
477 (((procfs_list_node_t *)(((char *)obj) + \
478 (procfs_list)->pl_node_offset))->pln_id)
481 procfs_list_add(procfs_list_t *procfs_list, void *p)
483 ASSERT(MUTEX_HELD(&procfs_list->pl_lock));
484 NODE_ID(procfs_list, p) = procfs_list->pl_next_id++;
485 list_insert_tail(&procfs_list->pl_list, p);
489 * =========================================================================
491 * =========================================================================
495 * =========================================================================
496 * Figure out which debugging statements to print
497 * =========================================================================
500 static char *dprintf_string;
501 static int dprintf_print_all;
504 dprintf_find_string(const char *string)
506 char *tmp_str = dprintf_string;
507 int len = strlen(string);
510 * Find out if this is a string we want to print.
511 * String format: file1.c,function_name1,file2.c,file3.c
514 while (tmp_str != NULL) {
515 if (strncmp(tmp_str, string, len) == 0 &&
516 (tmp_str[len] == ',' || tmp_str[len] == '\0'))
518 tmp_str = strchr(tmp_str, ',');
520 tmp_str++; /* Get rid of , */
526 dprintf_setup(int *argc, char **argv)
531 * Debugging can be specified two ways: by setting the
532 * environment variable ZFS_DEBUG, or by including a
533 * "debug=..." argument on the command line. The command
534 * line setting overrides the environment variable.
537 for (i = 1; i < *argc; i++) {
538 int len = strlen("debug=");
539 /* First look for a command line argument */
540 if (strncmp("debug=", argv[i], len) == 0) {
541 dprintf_string = argv[i] + len;
542 /* Remove from args */
543 for (j = i; j < *argc; j++)
550 if (dprintf_string == NULL) {
551 /* Look for ZFS_DEBUG environment variable */
552 dprintf_string = getenv("ZFS_DEBUG");
556 * Are we just turning on all debugging?
558 if (dprintf_find_string("on"))
559 dprintf_print_all = 1;
561 if (dprintf_string != NULL)
562 zfs_flags |= ZFS_DEBUG_DPRINTF;
566 * =========================================================================
568 * =========================================================================
571 __dprintf(boolean_t dprint, const char *file, const char *func,
572 int line, const char *fmt, ...)
578 * Get rid of annoying "../common/" prefix to filename.
580 newfile = strrchr(file, '/');
581 if (newfile != NULL) {
582 newfile = newfile + 1; /* Get rid of leading / */
588 /* dprintf messages are printed immediately */
590 if (!dprintf_print_all &&
591 !dprintf_find_string(newfile) &&
592 !dprintf_find_string(func))
595 /* Print out just the function name if requested */
597 if (dprintf_find_string("pid"))
598 (void) printf("%d ", getpid());
599 if (dprintf_find_string("tid"))
600 (void) printf("%ju ",
601 (uintmax_t)(uintptr_t)pthread_self());
602 if (dprintf_find_string("cpu"))
603 (void) printf("%u ", getcpuid());
604 if (dprintf_find_string("time"))
605 (void) printf("%llu ", gethrtime());
606 if (dprintf_find_string("long"))
607 (void) printf("%s, line %d: ", newfile, line);
608 (void) printf("dprintf: %s: ", func);
610 (void) vprintf(fmt, adx);
614 /* zfs_dbgmsg is logged for dumping later */
620 buf = umem_alloc(size, UMEM_NOFAIL);
621 i = snprintf(buf, size, "%s:%d:%s(): ", newfile, line, func);
625 (void) vsnprintf(buf + i, size - i, fmt, adx);
631 umem_free(buf, size);
636 * =========================================================================
637 * cmn_err() and panic()
638 * =========================================================================
640 static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
641 static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
644 vpanic(const char *fmt, va_list adx)
646 (void) fprintf(stderr, "error: ");
647 (void) vfprintf(stderr, fmt, adx);
648 (void) fprintf(stderr, "\n");
650 abort(); /* think of it as a "user-level crash dump" */
654 panic(const char *fmt, ...)
664 vcmn_err(int ce, const char *fmt, va_list adx)
668 if (ce != CE_NOTE) { /* suppress noise in userland stress testing */
669 (void) fprintf(stderr, "%s", ce_prefix[ce]);
670 (void) vfprintf(stderr, fmt, adx);
671 (void) fprintf(stderr, "%s", ce_suffix[ce]);
677 cmn_err(int ce, const char *fmt, ...)
682 vcmn_err(ce, fmt, adx);
687 * =========================================================================
689 * =========================================================================
695 (void) poll(0, 0, ticks * (1000 / hz));
699 * Find highest one bit set.
700 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
701 * The __builtin_clzll() function is supported by both GCC and Clang.
704 highbit64(uint64_t i)
709 return (NBBY * sizeof (uint64_t) - __builtin_clzll(i));
713 * Find lowest one bit set.
714 * Returns bit number + 1 of lowest bit that is set, otherwise returns 0.
715 * The __builtin_ffsll() function is supported by both GCC and Clang.
723 return (__builtin_ffsll(i));
726 char *random_path = "/dev/random";
727 char *urandom_path = "/dev/urandom";
728 static int random_fd = -1, urandom_fd = -1;
733 VERIFY((random_fd = open(random_path, O_RDONLY)) != -1);
734 VERIFY((urandom_fd = open(urandom_path, O_RDONLY)) != -1);
748 random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
756 bytes = read(fd, ptr, resid);
757 ASSERT3S(bytes, >=, 0);
766 random_get_bytes(uint8_t *ptr, size_t len)
768 return (random_get_bytes_common(ptr, len, random_fd));
772 random_get_pseudo_bytes(uint8_t *ptr, size_t len)
774 return (random_get_bytes_common(ptr, len, urandom_fd));
778 ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result)
782 *result = strtoul(hw_serial, &end, base);
789 ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
793 *result = strtoull(str, &end, base);
802 return (&hw_utsname);
806 * =========================================================================
807 * kernel emulation setup & teardown
808 * =========================================================================
811 umem_out_of_memory(void)
813 char errmsg[] = "out of memory -- generating core dump\n";
815 (void) fprintf(stderr, "%s", errmsg);
821 kernel_init(int mode)
823 extern uint_t rrw_tsd_key;
825 umem_nofail_callback(umem_out_of_memory);
827 physmem = sysconf(_SC_PHYS_PAGES);
829 dprintf("physmem = %llu pages (%.2f GB)\n", physmem,
830 (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
832 (void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
833 (mode & SPA_MODE_WRITE) ? get_system_hostid() : 0);
837 VERIFY0(uname(&hw_utsname));
844 spa_init((spa_mode_t)mode);
848 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
872 crgetruid(cred_t *cr)
884 crgetngroups(cred_t *cr)
890 crgetgroups(cred_t *cr)
896 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
902 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
908 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
914 secpolicy_zfs(const cred_t *cr)
920 secpolicy_zfs_proc(const cred_t *cr, proc_t *proc)
926 ksid_lookupdomain(const char *dom)
930 kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL);
931 kd->kd_name = spa_strdup(dom);
936 ksiddomain_rele(ksiddomain_t *ksid)
938 spa_strfree(ksid->kd_name);
939 umem_free(ksid, sizeof (ksiddomain_t));
943 kmem_vasprintf(const char *fmt, va_list adx)
948 va_copy(adx_copy, adx);
949 VERIFY(vasprintf(&buf, fmt, adx_copy) != -1);
956 kmem_asprintf(const char *fmt, ...)
962 VERIFY(vasprintf(&buf, fmt, adx) != -1);
970 zfs_onexit_fd_hold(int fd, minor_t *minorp)
978 zfs_onexit_fd_rele(int fd)
984 zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
985 uint64_t *action_handle)
991 spl_fstrans_mark(void)
993 return ((fstrans_cookie_t)0);
997 spl_fstrans_unmark(fstrans_cookie_t cookie)
1002 __spl_pf_fstrans_check(void)
1008 kmem_cache_reap_active(void)
1013 void *zvol_tag = "zvol_tag";
1016 zvol_create_minor(const char *name)
1021 zvol_create_minors_recursive(const char *name)
1026 zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
1031 zvol_rename_minors(spa_t *spa, const char *oldname, const char *newname,
1039 * path - fully qualified path to file
1040 * flags - file attributes O_READ / O_WRITE / O_EXCL
1041 * fpp - pointer to return file pointer
1043 * Returns 0 on success underlying error on failure.
1046 zfs_file_open(const char *path, int flags, int mode, zfs_file_t **fpp)
1055 if (!(flags & O_CREAT) && stat64(path, &st) == -1)
1058 if (!(flags & O_CREAT) && S_ISBLK(st.st_mode))
1061 if (flags & O_CREAT)
1062 old_umask = umask(0);
1064 fd = open64(path, flags, mode);
1068 if (flags & O_CREAT)
1069 (void) umask(old_umask);
1071 if (vn_dumpdir != NULL) {
1072 char *dumppath = umem_zalloc(MAXPATHLEN, UMEM_NOFAIL);
1073 char *inpath = basename((char *)(uintptr_t)path);
1075 (void) snprintf(dumppath, MAXPATHLEN,
1076 "%s/%s", vn_dumpdir, inpath);
1077 dump_fd = open64(dumppath, O_CREAT | O_WRONLY, 0666);
1078 umem_free(dumppath, MAXPATHLEN);
1079 if (dump_fd == -1) {
1088 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
1090 fp = umem_zalloc(sizeof (zfs_file_t), UMEM_NOFAIL);
1092 fp->f_dump_fd = dump_fd;
1099 zfs_file_close(zfs_file_t *fp)
1102 if (fp->f_dump_fd != -1)
1103 close(fp->f_dump_fd);
1105 umem_free(fp, sizeof (zfs_file_t));
1109 * Stateful write - use os internal file pointer to determine where to
1110 * write and update on successful completion.
1112 * fp - pointer to file (pipe, socket, etc) to write to
1113 * buf - buffer to write
1114 * count - # of bytes to write
1115 * resid - pointer to count of unwritten bytes (if short write)
1117 * Returns 0 on success errno on failure.
1120 zfs_file_write(zfs_file_t *fp, const void *buf, size_t count, ssize_t *resid)
1124 rc = write(fp->f_fd, buf, count);
1129 *resid = count - rc;
1130 } else if (rc != count) {
1138 * Stateless write - os internal file pointer is not updated.
1140 * fp - pointer to file (pipe, socket, etc) to write to
1141 * buf - buffer to write
1142 * count - # of bytes to write
1143 * off - file offset to write to (only valid for seekable types)
1144 * resid - pointer to count of unwritten bytes
1146 * Returns 0 on success errno on failure.
1149 zfs_file_pwrite(zfs_file_t *fp, const void *buf,
1150 size_t count, loff_t pos, ssize_t *resid)
1152 ssize_t rc, split, done;
1156 * To simulate partial disk writes, we split writes into two
1157 * system calls so that the process can be killed in between.
1158 * This is used by ztest to simulate realistic failure modes.
1160 sectors = count >> SPA_MINBLOCKSHIFT;
1161 split = (sectors > 0 ? rand() % sectors : 0) << SPA_MINBLOCKSHIFT;
1162 rc = pwrite64(fp->f_fd, buf, split, pos);
1165 rc = pwrite64(fp->f_fd, (char *)buf + split,
1166 count - split, pos + split);
1169 if (rc == -1 && errno == EINVAL) {
1171 * Under Linux, this most likely means an alignment issue
1172 * (memory or disk) due to O_DIRECT, so we abort() in order
1173 * to catch the offender.
1185 *resid = count - done;
1186 } else if (done != count) {
1194 * Stateful read - use os internal file pointer to determine where to
1195 * read and update on successful completion.
1197 * fp - pointer to file (pipe, socket, etc) to read from
1198 * buf - buffer to write
1199 * count - # of bytes to read
1200 * resid - pointer to count of unread bytes (if short read)
1202 * Returns 0 on success errno on failure.
1205 zfs_file_read(zfs_file_t *fp, void *buf, size_t count, ssize_t *resid)
1209 rc = read(fp->f_fd, buf, count);
1214 *resid = count - rc;
1215 } else if (rc != count) {
1223 * Stateless read - os internal file pointer is not updated.
1225 * fp - pointer to file (pipe, socket, etc) to read from
1226 * buf - buffer to write
1227 * count - # of bytes to write
1228 * off - file offset to read from (only valid for seekable types)
1229 * resid - pointer to count of unwritten bytes (if short write)
1231 * Returns 0 on success errno on failure.
1234 zfs_file_pread(zfs_file_t *fp, void *buf, size_t count, loff_t off,
1239 rc = pread64(fp->f_fd, buf, count, off);
1243 * Under Linux, this most likely means an alignment issue
1244 * (memory or disk) due to O_DIRECT, so we abort() in order to
1245 * catch the offender.
1247 if (errno == EINVAL)
1253 if (fp->f_dump_fd != -1) {
1256 status = pwrite64(fp->f_dump_fd, buf, rc, off);
1257 ASSERT(status != -1);
1261 *resid = count - rc;
1262 } else if (rc != count) {
1270 * lseek - set / get file pointer
1272 * fp - pointer to file (pipe, socket, etc) to read from
1273 * offp - value to seek to, returns current value plus passed offset
1274 * whence - see man pages for standard lseek whence values
1276 * Returns 0 on success errno on failure (ESPIPE for non seekable types)
1279 zfs_file_seek(zfs_file_t *fp, loff_t *offp, int whence)
1283 rc = lseek(fp->f_fd, *offp, whence);
1293 * Get file attributes
1295 * filp - file pointer
1296 * zfattr - pointer to file attr structure
1298 * Currently only used for fetching size and file mode
1300 * Returns 0 on success or error code of underlying getattr call on failure.
1303 zfs_file_getattr(zfs_file_t *fp, zfs_file_attr_t *zfattr)
1307 if (fstat64_blk(fp->f_fd, &st) == -1)
1310 zfattr->zfa_size = st.st_size;
1311 zfattr->zfa_mode = st.st_mode;
1319 * filp - file pointer
1320 * flags - O_SYNC and or O_DSYNC
1322 * Returns 0 on success or error code of underlying sync call on failure.
1325 zfs_file_fsync(zfs_file_t *fp, int flags)
1329 rc = fsync(fp->f_fd);
1337 * fallocate - allocate or free space on disk
1340 * mode (non-standard options for hole punching etc)
1341 * offset - offset to start allocating or freeing from
1342 * len - length to free / allocate
1347 zfs_file_fallocate(zfs_file_t *fp, int mode, loff_t offset, loff_t len)
1350 return (fallocate(fp->f_fd, mode, offset, len));
1352 return (EOPNOTSUPP);
1357 * Request current file pointer offset
1359 * fp - pointer to file
1361 * Returns current file offset.
1364 zfs_file_off(zfs_file_t *fp)
1366 return (lseek(fp->f_fd, SEEK_CUR, 0));
1372 * path - fully qualified file path
1374 * Returns 0 on success.
1379 zfs_file_unlink(const char *path)
1381 return (remove(path));
1385 * Get reference to file pointer
1387 * fd - input file descriptor
1388 * fpp - pointer to file pointer
1390 * Returns 0 on success EBADF on failure.
1391 * Unsupported in user space.
1394 zfs_file_get(int fd, zfs_file_t **fpp)
1398 return (EOPNOTSUPP);
1402 * Drop reference to file pointer
1404 * fd - input file descriptor
1406 * Unsupported in user space.
1409 zfs_file_put(int fd)
1415 zfsvfs_update_fromname(const char *oldname, const char *newname)