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/zstd/zstd.h>
46 #include <zfs_fletcher.h>
50 * Emulation of kernel services in userland.
54 char hw_serial[HW_HOSTID_LEN];
55 struct utsname hw_utsname;
57 /* If set, all blocks read will be copied to the specified directory. */
58 char *vn_dumpdir = NULL;
60 /* this only exists to have its address taken */
64 * =========================================================================
66 * =========================================================================
68 * TS_STACK_MIN is dictated by the minimum allowed pthread stack size. While
69 * TS_STACK_MAX is somewhat arbitrary, it was selected to be large enough for
70 * the expected stack depth while small enough to avoid exhausting address
71 * space with high thread counts.
73 #define TS_STACK_MIN MAX(PTHREAD_STACK_MIN, 32768)
74 #define TS_STACK_MAX (256 * 1024)
78 zk_thread_create(void (*func)(void *), void *arg, size_t stksize, int state)
83 int detachstate = PTHREAD_CREATE_DETACHED;
85 VERIFY0(pthread_attr_init(&attr));
87 if (state & TS_JOINABLE)
88 detachstate = PTHREAD_CREATE_JOINABLE;
90 VERIFY0(pthread_attr_setdetachstate(&attr, detachstate));
93 * We allow the default stack size in user space to be specified by
94 * setting the ZFS_STACK_SIZE environment variable. This allows us
95 * the convenience of observing and debugging stack overruns in
96 * user space. Explicitly specified stack sizes will be honored.
97 * The usage of ZFS_STACK_SIZE is discussed further in the
98 * ENVIRONMENT VARIABLES sections of the ztest(1) man page.
101 stkstr = getenv("ZFS_STACK_SIZE");
104 stksize = TS_STACK_MAX;
106 stksize = MAX(atoi(stkstr), TS_STACK_MIN);
109 VERIFY3S(stksize, >, 0);
110 stksize = P2ROUNDUP(MAX(stksize, TS_STACK_MIN), PAGESIZE);
113 * If this ever fails, it may be because the stack size is not a
114 * multiple of system page size.
116 VERIFY0(pthread_attr_setstacksize(&attr, stksize));
117 VERIFY0(pthread_attr_setguardsize(&attr, PAGESIZE));
119 VERIFY0(pthread_create(&tid, &attr, (void *(*)(void *))func, arg));
120 VERIFY0(pthread_attr_destroy(&attr));
122 return ((void *)(uintptr_t)tid);
126 * =========================================================================
128 * =========================================================================
132 kstat_create(const char *module, int instance, const char *name,
133 const char *class, uchar_t type, ulong_t ndata, uchar_t ks_flag)
140 kstat_install(kstat_t *ksp)
145 kstat_delete(kstat_t *ksp)
150 kstat_waitq_enter(kstat_io_t *kiop)
155 kstat_waitq_exit(kstat_io_t *kiop)
160 kstat_runq_enter(kstat_io_t *kiop)
165 kstat_runq_exit(kstat_io_t *kiop)
170 kstat_waitq_to_runq(kstat_io_t *kiop)
175 kstat_runq_back_to_waitq(kstat_io_t *kiop)
179 kstat_set_raw_ops(kstat_t *ksp,
180 int (*headers)(char *buf, size_t size),
181 int (*data)(char *buf, size_t size, void *data),
182 void *(*addr)(kstat_t *ksp, loff_t index))
186 * =========================================================================
188 * =========================================================================
192 mutex_init(kmutex_t *mp, char *name, int type, void *cookie)
194 VERIFY0(pthread_mutex_init(&mp->m_lock, NULL));
195 memset(&mp->m_owner, 0, sizeof (pthread_t));
199 mutex_destroy(kmutex_t *mp)
201 VERIFY0(pthread_mutex_destroy(&mp->m_lock));
205 mutex_enter(kmutex_t *mp)
207 VERIFY0(pthread_mutex_lock(&mp->m_lock));
208 mp->m_owner = pthread_self();
212 mutex_tryenter(kmutex_t *mp)
216 error = pthread_mutex_trylock(&mp->m_lock);
218 mp->m_owner = pthread_self();
221 VERIFY3S(error, ==, EBUSY);
227 mutex_exit(kmutex_t *mp)
229 memset(&mp->m_owner, 0, sizeof (pthread_t));
230 VERIFY0(pthread_mutex_unlock(&mp->m_lock));
234 * =========================================================================
236 * =========================================================================
240 rw_init(krwlock_t *rwlp, char *name, int type, void *arg)
242 VERIFY0(pthread_rwlock_init(&rwlp->rw_lock, NULL));
243 rwlp->rw_readers = 0;
248 rw_destroy(krwlock_t *rwlp)
250 VERIFY0(pthread_rwlock_destroy(&rwlp->rw_lock));
254 rw_enter(krwlock_t *rwlp, krw_t rw)
256 if (rw == RW_READER) {
257 VERIFY0(pthread_rwlock_rdlock(&rwlp->rw_lock));
258 atomic_inc_uint(&rwlp->rw_readers);
260 VERIFY0(pthread_rwlock_wrlock(&rwlp->rw_lock));
261 rwlp->rw_owner = pthread_self();
266 rw_exit(krwlock_t *rwlp)
268 if (RW_READ_HELD(rwlp))
269 atomic_dec_uint(&rwlp->rw_readers);
273 VERIFY0(pthread_rwlock_unlock(&rwlp->rw_lock));
277 rw_tryenter(krwlock_t *rwlp, krw_t rw)
282 error = pthread_rwlock_tryrdlock(&rwlp->rw_lock);
284 error = pthread_rwlock_trywrlock(&rwlp->rw_lock);
288 atomic_inc_uint(&rwlp->rw_readers);
290 rwlp->rw_owner = pthread_self();
295 VERIFY3S(error, ==, EBUSY);
302 zone_get_hostid(void *zonep)
305 * We're emulating the system's hostid in userland.
307 return (strtoul(hw_serial, NULL, 10));
311 rw_tryupgrade(krwlock_t *rwlp)
317 * =========================================================================
318 * condition variables
319 * =========================================================================
323 cv_init(kcondvar_t *cv, char *name, int type, void *arg)
325 VERIFY0(pthread_cond_init(cv, NULL));
329 cv_destroy(kcondvar_t *cv)
331 VERIFY0(pthread_cond_destroy(cv));
335 cv_wait(kcondvar_t *cv, kmutex_t *mp)
337 memset(&mp->m_owner, 0, sizeof (pthread_t));
338 VERIFY0(pthread_cond_wait(cv, &mp->m_lock));
339 mp->m_owner = pthread_self();
343 cv_wait_sig(kcondvar_t *cv, kmutex_t *mp)
350 cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
357 delta = abstime - ddi_get_lbolt();
361 VERIFY(gettimeofday(&tv, NULL) == 0);
363 ts.tv_sec = tv.tv_sec + delta / hz;
364 ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % hz) * (NANOSEC / hz);
365 if (ts.tv_nsec >= NANOSEC) {
367 ts.tv_nsec -= NANOSEC;
370 memset(&mp->m_owner, 0, sizeof (pthread_t));
371 error = pthread_cond_timedwait(cv, &mp->m_lock, &ts);
372 mp->m_owner = pthread_self();
374 if (error == ETIMEDOUT)
384 cv_timedwait_hires(kcondvar_t *cv, kmutex_t *mp, hrtime_t tim, hrtime_t res,
392 ASSERT(flag == 0 || flag == CALLOUT_FLAG_ABSOLUTE);
395 if (flag & CALLOUT_FLAG_ABSOLUTE)
396 delta -= gethrtime();
401 VERIFY0(gettimeofday(&tv, NULL));
403 ts.tv_sec = tv.tv_sec + delta / NANOSEC;
404 ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC + (delta % NANOSEC);
405 if (ts.tv_nsec >= NANOSEC) {
407 ts.tv_nsec -= NANOSEC;
410 memset(&mp->m_owner, 0, sizeof (pthread_t));
411 error = pthread_cond_timedwait(cv, &mp->m_lock, &ts);
412 mp->m_owner = pthread_self();
414 if (error == ETIMEDOUT)
423 cv_signal(kcondvar_t *cv)
425 VERIFY0(pthread_cond_signal(cv));
429 cv_broadcast(kcondvar_t *cv)
431 VERIFY0(pthread_cond_broadcast(cv));
435 * =========================================================================
437 * =========================================================================
441 seq_printf(struct seq_file *m, const char *fmt, ...)
445 procfs_list_install(const char *module,
448 procfs_list_t *procfs_list,
449 int (*show)(struct seq_file *f, void *p),
450 int (*show_header)(struct seq_file *f),
451 int (*clear)(procfs_list_t *procfs_list),
452 size_t procfs_list_node_off)
454 mutex_init(&procfs_list->pl_lock, NULL, MUTEX_DEFAULT, NULL);
455 list_create(&procfs_list->pl_list,
456 procfs_list_node_off + sizeof (procfs_list_node_t),
457 procfs_list_node_off + offsetof(procfs_list_node_t, pln_link));
458 procfs_list->pl_next_id = 1;
459 procfs_list->pl_node_offset = procfs_list_node_off;
463 procfs_list_uninstall(procfs_list_t *procfs_list)
467 procfs_list_destroy(procfs_list_t *procfs_list)
469 ASSERT(list_is_empty(&procfs_list->pl_list));
470 list_destroy(&procfs_list->pl_list);
471 mutex_destroy(&procfs_list->pl_lock);
474 #define NODE_ID(procfs_list, obj) \
475 (((procfs_list_node_t *)(((char *)obj) + \
476 (procfs_list)->pl_node_offset))->pln_id)
479 procfs_list_add(procfs_list_t *procfs_list, void *p)
481 ASSERT(MUTEX_HELD(&procfs_list->pl_lock));
482 NODE_ID(procfs_list, p) = procfs_list->pl_next_id++;
483 list_insert_tail(&procfs_list->pl_list, p);
487 * =========================================================================
489 * =========================================================================
493 * =========================================================================
494 * Figure out which debugging statements to print
495 * =========================================================================
498 static char *dprintf_string;
499 static int dprintf_print_all;
502 dprintf_find_string(const char *string)
504 char *tmp_str = dprintf_string;
505 int len = strlen(string);
508 * Find out if this is a string we want to print.
509 * String format: file1.c,function_name1,file2.c,file3.c
512 while (tmp_str != NULL) {
513 if (strncmp(tmp_str, string, len) == 0 &&
514 (tmp_str[len] == ',' || tmp_str[len] == '\0'))
516 tmp_str = strchr(tmp_str, ',');
518 tmp_str++; /* Get rid of , */
524 dprintf_setup(int *argc, char **argv)
529 * Debugging can be specified two ways: by setting the
530 * environment variable ZFS_DEBUG, or by including a
531 * "debug=..." argument on the command line. The command
532 * line setting overrides the environment variable.
535 for (i = 1; i < *argc; i++) {
536 int len = strlen("debug=");
537 /* First look for a command line argument */
538 if (strncmp("debug=", argv[i], len) == 0) {
539 dprintf_string = argv[i] + len;
540 /* Remove from args */
541 for (j = i; j < *argc; j++)
548 if (dprintf_string == NULL) {
549 /* Look for ZFS_DEBUG environment variable */
550 dprintf_string = getenv("ZFS_DEBUG");
554 * Are we just turning on all debugging?
556 if (dprintf_find_string("on"))
557 dprintf_print_all = 1;
559 if (dprintf_string != NULL)
560 zfs_flags |= ZFS_DEBUG_DPRINTF;
564 * =========================================================================
566 * =========================================================================
569 __dprintf(boolean_t dprint, const char *file, const char *func,
570 int line, const char *fmt, ...)
576 * Get rid of annoying "../common/" prefix to filename.
578 newfile = strrchr(file, '/');
579 if (newfile != NULL) {
580 newfile = newfile + 1; /* Get rid of leading / */
586 /* dprintf messages are printed immediately */
588 if (!dprintf_print_all &&
589 !dprintf_find_string(newfile) &&
590 !dprintf_find_string(func))
593 /* Print out just the function name if requested */
595 if (dprintf_find_string("pid"))
596 (void) printf("%d ", getpid());
597 if (dprintf_find_string("tid"))
598 (void) printf("%ju ",
599 (uintmax_t)(uintptr_t)pthread_self());
600 if (dprintf_find_string("cpu"))
601 (void) printf("%u ", getcpuid());
602 if (dprintf_find_string("time"))
603 (void) printf("%llu ", gethrtime());
604 if (dprintf_find_string("long"))
605 (void) printf("%s, line %d: ", newfile, line);
606 (void) printf("dprintf: %s: ", func);
608 (void) vprintf(fmt, adx);
612 /* zfs_dbgmsg is logged for dumping later */
618 buf = umem_alloc(size, UMEM_NOFAIL);
619 i = snprintf(buf, size, "%s:%d:%s(): ", newfile, line, func);
623 (void) vsnprintf(buf + i, size - i, fmt, adx);
629 umem_free(buf, size);
634 * =========================================================================
635 * cmn_err() and panic()
636 * =========================================================================
638 static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
639 static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
642 vpanic(const char *fmt, va_list adx)
644 (void) fprintf(stderr, "error: ");
645 (void) vfprintf(stderr, fmt, adx);
646 (void) fprintf(stderr, "\n");
648 abort(); /* think of it as a "user-level crash dump" */
652 panic(const char *fmt, ...)
662 vcmn_err(int ce, const char *fmt, va_list adx)
666 if (ce != CE_NOTE) { /* suppress noise in userland stress testing */
667 (void) fprintf(stderr, "%s", ce_prefix[ce]);
668 (void) vfprintf(stderr, fmt, adx);
669 (void) fprintf(stderr, "%s", ce_suffix[ce]);
675 cmn_err(int ce, const char *fmt, ...)
680 vcmn_err(ce, fmt, adx);
685 * =========================================================================
687 * =========================================================================
693 (void) poll(0, 0, ticks * (1000 / hz));
697 * Find highest one bit set.
698 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
699 * The __builtin_clzll() function is supported by both GCC and Clang.
702 highbit64(uint64_t i)
707 return (NBBY * sizeof (uint64_t) - __builtin_clzll(i));
711 * Find lowest one bit set.
712 * Returns bit number + 1 of lowest bit that is set, otherwise returns 0.
713 * The __builtin_ffsll() function is supported by both GCC and Clang.
721 return (__builtin_ffsll(i));
724 char *random_path = "/dev/random";
725 char *urandom_path = "/dev/urandom";
726 static int random_fd = -1, urandom_fd = -1;
731 VERIFY((random_fd = open(random_path, O_RDONLY)) != -1);
732 VERIFY((urandom_fd = open(urandom_path, O_RDONLY)) != -1);
746 random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
754 bytes = read(fd, ptr, resid);
755 ASSERT3S(bytes, >=, 0);
764 random_get_bytes(uint8_t *ptr, size_t len)
766 return (random_get_bytes_common(ptr, len, random_fd));
770 random_get_pseudo_bytes(uint8_t *ptr, size_t len)
772 return (random_get_bytes_common(ptr, len, urandom_fd));
776 ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result)
780 *result = strtoul(hw_serial, &end, base);
787 ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
791 *result = strtoull(str, &end, base);
800 return (&hw_utsname);
804 * =========================================================================
805 * kernel emulation setup & teardown
806 * =========================================================================
809 umem_out_of_memory(void)
811 char errmsg[] = "out of memory -- generating core dump\n";
813 (void) fprintf(stderr, "%s", errmsg);
819 kernel_init(int mode)
821 extern uint_t rrw_tsd_key;
823 umem_nofail_callback(umem_out_of_memory);
825 physmem = sysconf(_SC_PHYS_PAGES);
827 dprintf("physmem = %llu pages (%.2f GB)\n", physmem,
828 (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
830 (void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
831 (mode & SPA_MODE_WRITE) ? get_system_hostid() : 0);
835 VERIFY0(uname(&hw_utsname));
842 spa_init((spa_mode_t)mode);
846 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
870 crgetruid(cred_t *cr)
882 crgetngroups(cred_t *cr)
888 crgetgroups(cred_t *cr)
894 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
900 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
906 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
912 secpolicy_zfs(const cred_t *cr)
918 secpolicy_zfs_proc(const cred_t *cr, proc_t *proc)
924 ksid_lookupdomain(const char *dom)
928 kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL);
929 kd->kd_name = spa_strdup(dom);
934 ksiddomain_rele(ksiddomain_t *ksid)
936 spa_strfree(ksid->kd_name);
937 umem_free(ksid, sizeof (ksiddomain_t));
941 kmem_vasprintf(const char *fmt, va_list adx)
946 va_copy(adx_copy, adx);
947 VERIFY(vasprintf(&buf, fmt, adx_copy) != -1);
954 kmem_asprintf(const char *fmt, ...)
960 VERIFY(vasprintf(&buf, fmt, adx) != -1);
968 zfs_onexit_fd_hold(int fd, minor_t *minorp)
976 zfs_onexit_fd_rele(int fd)
982 zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
983 uint64_t *action_handle)
989 spl_fstrans_mark(void)
991 return ((fstrans_cookie_t)0);
995 spl_fstrans_unmark(fstrans_cookie_t cookie)
1000 __spl_pf_fstrans_check(void)
1006 kmem_cache_reap_active(void)
1011 void *zvol_tag = "zvol_tag";
1014 zvol_create_minor(const char *name)
1019 zvol_create_minors_recursive(const char *name)
1024 zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
1029 zvol_rename_minors(spa_t *spa, const char *oldname, const char *newname,
1037 * path - fully qualified path to file
1038 * flags - file attributes O_READ / O_WRITE / O_EXCL
1039 * fpp - pointer to return file pointer
1041 * Returns 0 on success underlying error on failure.
1044 zfs_file_open(const char *path, int flags, int mode, zfs_file_t **fpp)
1053 if (!(flags & O_CREAT) && stat64(path, &st) == -1)
1056 if (!(flags & O_CREAT) && S_ISBLK(st.st_mode))
1059 if (flags & O_CREAT)
1060 old_umask = umask(0);
1062 fd = open64(path, flags, mode);
1066 if (flags & O_CREAT)
1067 (void) umask(old_umask);
1069 if (vn_dumpdir != NULL) {
1070 char *dumppath = umem_zalloc(MAXPATHLEN, UMEM_NOFAIL);
1071 char *inpath = basename((char *)(uintptr_t)path);
1073 (void) snprintf(dumppath, MAXPATHLEN,
1074 "%s/%s", vn_dumpdir, inpath);
1075 dump_fd = open64(dumppath, O_CREAT | O_WRONLY, 0666);
1076 umem_free(dumppath, MAXPATHLEN);
1077 if (dump_fd == -1) {
1086 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
1088 fp = umem_zalloc(sizeof (zfs_file_t), UMEM_NOFAIL);
1090 fp->f_dump_fd = dump_fd;
1097 zfs_file_close(zfs_file_t *fp)
1100 if (fp->f_dump_fd != -1)
1101 close(fp->f_dump_fd);
1103 umem_free(fp, sizeof (zfs_file_t));
1107 * Stateful write - use os internal file pointer to determine where to
1108 * write and update on successful completion.
1110 * fp - pointer to file (pipe, socket, etc) to write to
1111 * buf - buffer to write
1112 * count - # of bytes to write
1113 * resid - pointer to count of unwritten bytes (if short write)
1115 * Returns 0 on success errno on failure.
1118 zfs_file_write(zfs_file_t *fp, const void *buf, size_t count, ssize_t *resid)
1122 rc = write(fp->f_fd, buf, count);
1127 *resid = count - rc;
1128 } else if (rc != count) {
1136 * Stateless write - os internal file pointer is not updated.
1138 * fp - pointer to file (pipe, socket, etc) to write to
1139 * buf - buffer to write
1140 * count - # of bytes to write
1141 * off - file offset to write to (only valid for seekable types)
1142 * resid - pointer to count of unwritten bytes
1144 * Returns 0 on success errno on failure.
1147 zfs_file_pwrite(zfs_file_t *fp, const void *buf,
1148 size_t count, loff_t pos, ssize_t *resid)
1150 ssize_t rc, split, done;
1154 * To simulate partial disk writes, we split writes into two
1155 * system calls so that the process can be killed in between.
1156 * This is used by ztest to simulate realistic failure modes.
1158 sectors = count >> SPA_MINBLOCKSHIFT;
1159 split = (sectors > 0 ? rand() % sectors : 0) << SPA_MINBLOCKSHIFT;
1160 rc = pwrite64(fp->f_fd, buf, split, pos);
1163 rc = pwrite64(fp->f_fd, (char *)buf + split,
1164 count - split, pos + split);
1167 if (rc == -1 && errno == EINVAL) {
1169 * Under Linux, this most likely means an alignment issue
1170 * (memory or disk) due to O_DIRECT, so we abort() in order
1171 * to catch the offender.
1183 *resid = count - done;
1184 } else if (done != count) {
1192 * Stateful read - use os internal file pointer to determine where to
1193 * read and update on successful completion.
1195 * fp - pointer to file (pipe, socket, etc) to read from
1196 * buf - buffer to write
1197 * count - # of bytes to read
1198 * resid - pointer to count of unread bytes (if short read)
1200 * Returns 0 on success errno on failure.
1203 zfs_file_read(zfs_file_t *fp, void *buf, size_t count, ssize_t *resid)
1207 rc = read(fp->f_fd, buf, count);
1212 *resid = count - rc;
1213 } else if (rc != count) {
1221 * Stateless read - os internal file pointer is not updated.
1223 * fp - pointer to file (pipe, socket, etc) to read from
1224 * buf - buffer to write
1225 * count - # of bytes to write
1226 * off - file offset to read from (only valid for seekable types)
1227 * resid - pointer to count of unwritten bytes (if short write)
1229 * Returns 0 on success errno on failure.
1232 zfs_file_pread(zfs_file_t *fp, void *buf, size_t count, loff_t off,
1237 rc = pread64(fp->f_fd, buf, count, off);
1241 * Under Linux, this most likely means an alignment issue
1242 * (memory or disk) due to O_DIRECT, so we abort() in order to
1243 * catch the offender.
1245 if (errno == EINVAL)
1251 if (fp->f_dump_fd != -1) {
1254 status = pwrite64(fp->f_dump_fd, buf, rc, off);
1255 ASSERT(status != -1);
1259 *resid = count - rc;
1260 } else if (rc != count) {
1268 * lseek - set / get file pointer
1270 * fp - pointer to file (pipe, socket, etc) to read from
1271 * offp - value to seek to, returns current value plus passed offset
1272 * whence - see man pages for standard lseek whence values
1274 * Returns 0 on success errno on failure (ESPIPE for non seekable types)
1277 zfs_file_seek(zfs_file_t *fp, loff_t *offp, int whence)
1281 rc = lseek(fp->f_fd, *offp, whence);
1291 * Get file attributes
1293 * filp - file pointer
1294 * zfattr - pointer to file attr structure
1296 * Currently only used for fetching size and file mode
1298 * Returns 0 on success or error code of underlying getattr call on failure.
1301 zfs_file_getattr(zfs_file_t *fp, zfs_file_attr_t *zfattr)
1305 if (fstat64_blk(fp->f_fd, &st) == -1)
1308 zfattr->zfa_size = st.st_size;
1309 zfattr->zfa_mode = st.st_mode;
1317 * filp - file pointer
1318 * flags - O_SYNC and or O_DSYNC
1320 * Returns 0 on success or error code of underlying sync call on failure.
1323 zfs_file_fsync(zfs_file_t *fp, int flags)
1327 rc = fsync(fp->f_fd);
1335 * fallocate - allocate or free space on disk
1338 * mode (non-standard options for hole punching etc)
1339 * offset - offset to start allocating or freeing from
1340 * len - length to free / allocate
1345 zfs_file_fallocate(zfs_file_t *fp, int mode, loff_t offset, loff_t len)
1348 return (fallocate(fp->f_fd, mode, offset, len));
1350 return (EOPNOTSUPP);
1355 * Request current file pointer offset
1357 * fp - pointer to file
1359 * Returns current file offset.
1362 zfs_file_off(zfs_file_t *fp)
1364 return (lseek(fp->f_fd, SEEK_CUR, 0));
1370 * path - fully qualified file path
1372 * Returns 0 on success.
1377 zfs_file_unlink(const char *path)
1379 return (remove(path));
1383 * Get reference to file pointer
1385 * fd - input file descriptor
1386 * fpp - pointer to file pointer
1388 * Returns 0 on success EBADF on failure.
1389 * Unsupported in user space.
1392 zfs_file_get(int fd, zfs_file_t **fpp)
1396 return (EOPNOTSUPP);
1400 * Drop reference to file pointer
1402 * fd - input file descriptor
1404 * Unsupported in user space.
1407 zfs_file_put(int fd)