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,
449 procfs_list_t *procfs_list,
450 int (*show)(struct seq_file *f, void *p),
451 int (*show_header)(struct seq_file *f),
452 int (*clear)(procfs_list_t *procfs_list),
453 size_t procfs_list_node_off)
455 mutex_init(&procfs_list->pl_lock, NULL, MUTEX_DEFAULT, NULL);
456 list_create(&procfs_list->pl_list,
457 procfs_list_node_off + sizeof (procfs_list_node_t),
458 procfs_list_node_off + offsetof(procfs_list_node_t, pln_link));
459 procfs_list->pl_next_id = 1;
460 procfs_list->pl_node_offset = procfs_list_node_off;
464 procfs_list_uninstall(procfs_list_t *procfs_list)
468 procfs_list_destroy(procfs_list_t *procfs_list)
470 ASSERT(list_is_empty(&procfs_list->pl_list));
471 list_destroy(&procfs_list->pl_list);
472 mutex_destroy(&procfs_list->pl_lock);
475 #define NODE_ID(procfs_list, obj) \
476 (((procfs_list_node_t *)(((char *)obj) + \
477 (procfs_list)->pl_node_offset))->pln_id)
480 procfs_list_add(procfs_list_t *procfs_list, void *p)
482 ASSERT(MUTEX_HELD(&procfs_list->pl_lock));
483 NODE_ID(procfs_list, p) = procfs_list->pl_next_id++;
484 list_insert_tail(&procfs_list->pl_list, p);
488 * =========================================================================
490 * =========================================================================
494 * =========================================================================
495 * Figure out which debugging statements to print
496 * =========================================================================
499 static char *dprintf_string;
500 static int dprintf_print_all;
503 dprintf_find_string(const char *string)
505 char *tmp_str = dprintf_string;
506 int len = strlen(string);
509 * Find out if this is a string we want to print.
510 * String format: file1.c,function_name1,file2.c,file3.c
513 while (tmp_str != NULL) {
514 if (strncmp(tmp_str, string, len) == 0 &&
515 (tmp_str[len] == ',' || tmp_str[len] == '\0'))
517 tmp_str = strchr(tmp_str, ',');
519 tmp_str++; /* Get rid of , */
525 dprintf_setup(int *argc, char **argv)
530 * Debugging can be specified two ways: by setting the
531 * environment variable ZFS_DEBUG, or by including a
532 * "debug=..." argument on the command line. The command
533 * line setting overrides the environment variable.
536 for (i = 1; i < *argc; i++) {
537 int len = strlen("debug=");
538 /* First look for a command line argument */
539 if (strncmp("debug=", argv[i], len) == 0) {
540 dprintf_string = argv[i] + len;
541 /* Remove from args */
542 for (j = i; j < *argc; j++)
549 if (dprintf_string == NULL) {
550 /* Look for ZFS_DEBUG environment variable */
551 dprintf_string = getenv("ZFS_DEBUG");
555 * Are we just turning on all debugging?
557 if (dprintf_find_string("on"))
558 dprintf_print_all = 1;
560 if (dprintf_string != NULL)
561 zfs_flags |= ZFS_DEBUG_DPRINTF;
565 * =========================================================================
567 * =========================================================================
570 __dprintf(boolean_t dprint, const char *file, const char *func,
571 int line, const char *fmt, ...)
577 * Get rid of annoying "../common/" prefix to filename.
579 newfile = strrchr(file, '/');
580 if (newfile != NULL) {
581 newfile = newfile + 1; /* Get rid of leading / */
587 /* dprintf messages are printed immediately */
589 if (!dprintf_print_all &&
590 !dprintf_find_string(newfile) &&
591 !dprintf_find_string(func))
594 /* Print out just the function name if requested */
596 if (dprintf_find_string("pid"))
597 (void) printf("%d ", getpid());
598 if (dprintf_find_string("tid"))
599 (void) printf("%ju ",
600 (uintmax_t)(uintptr_t)pthread_self());
601 if (dprintf_find_string("cpu"))
602 (void) printf("%u ", getcpuid());
603 if (dprintf_find_string("time"))
604 (void) printf("%llu ", gethrtime());
605 if (dprintf_find_string("long"))
606 (void) printf("%s, line %d: ", newfile, line);
607 (void) printf("dprintf: %s: ", func);
609 (void) vprintf(fmt, adx);
613 /* zfs_dbgmsg is logged for dumping later */
619 buf = umem_alloc(size, UMEM_NOFAIL);
620 i = snprintf(buf, size, "%s:%d:%s(): ", newfile, line, func);
624 (void) vsnprintf(buf + i, size - i, fmt, adx);
630 umem_free(buf, size);
635 * =========================================================================
636 * cmn_err() and panic()
637 * =========================================================================
639 static char ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
640 static char ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
643 vpanic(const char *fmt, va_list adx)
645 (void) fprintf(stderr, "error: ");
646 (void) vfprintf(stderr, fmt, adx);
647 (void) fprintf(stderr, "\n");
649 abort(); /* think of it as a "user-level crash dump" */
653 panic(const char *fmt, ...)
663 vcmn_err(int ce, const char *fmt, va_list adx)
667 if (ce != CE_NOTE) { /* suppress noise in userland stress testing */
668 (void) fprintf(stderr, "%s", ce_prefix[ce]);
669 (void) vfprintf(stderr, fmt, adx);
670 (void) fprintf(stderr, "%s", ce_suffix[ce]);
676 cmn_err(int ce, const char *fmt, ...)
681 vcmn_err(ce, fmt, adx);
686 * =========================================================================
688 * =========================================================================
694 (void) poll(0, 0, ticks * (1000 / hz));
698 * Find highest one bit set.
699 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
700 * The __builtin_clzll() function is supported by both GCC and Clang.
703 highbit64(uint64_t i)
708 return (NBBY * sizeof (uint64_t) - __builtin_clzll(i));
712 * Find lowest one bit set.
713 * Returns bit number + 1 of lowest bit that is set, otherwise returns 0.
714 * The __builtin_ffsll() function is supported by both GCC and Clang.
722 return (__builtin_ffsll(i));
725 char *random_path = "/dev/random";
726 char *urandom_path = "/dev/urandom";
727 static int random_fd = -1, urandom_fd = -1;
732 VERIFY((random_fd = open(random_path, O_RDONLY)) != -1);
733 VERIFY((urandom_fd = open(urandom_path, O_RDONLY)) != -1);
747 random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
755 bytes = read(fd, ptr, resid);
756 ASSERT3S(bytes, >=, 0);
765 random_get_bytes(uint8_t *ptr, size_t len)
767 return (random_get_bytes_common(ptr, len, random_fd));
771 random_get_pseudo_bytes(uint8_t *ptr, size_t len)
773 return (random_get_bytes_common(ptr, len, urandom_fd));
777 ddi_strtoul(const char *hw_serial, char **nptr, int base, unsigned long *result)
781 *result = strtoul(hw_serial, &end, base);
788 ddi_strtoull(const char *str, char **nptr, int base, u_longlong_t *result)
792 *result = strtoull(str, &end, base);
801 return (&hw_utsname);
805 * =========================================================================
806 * kernel emulation setup & teardown
807 * =========================================================================
810 umem_out_of_memory(void)
812 char errmsg[] = "out of memory -- generating core dump\n";
814 (void) fprintf(stderr, "%s", errmsg);
820 kernel_init(int mode)
822 extern uint_t rrw_tsd_key;
824 umem_nofail_callback(umem_out_of_memory);
826 physmem = sysconf(_SC_PHYS_PAGES);
828 dprintf("physmem = %llu pages (%.2f GB)\n", physmem,
829 (double)physmem * sysconf(_SC_PAGE_SIZE) / (1ULL << 30));
831 (void) snprintf(hw_serial, sizeof (hw_serial), "%ld",
832 (mode & SPA_MODE_WRITE) ? get_system_hostid() : 0);
836 VERIFY0(uname(&hw_utsname));
843 spa_init((spa_mode_t)mode);
847 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
871 crgetruid(cred_t *cr)
883 crgetngroups(cred_t *cr)
889 crgetgroups(cred_t *cr)
895 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
901 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
907 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
913 secpolicy_zfs(const cred_t *cr)
919 secpolicy_zfs_proc(const cred_t *cr, proc_t *proc)
925 ksid_lookupdomain(const char *dom)
929 kd = umem_zalloc(sizeof (ksiddomain_t), UMEM_NOFAIL);
930 kd->kd_name = spa_strdup(dom);
935 ksiddomain_rele(ksiddomain_t *ksid)
937 spa_strfree(ksid->kd_name);
938 umem_free(ksid, sizeof (ksiddomain_t));
942 kmem_vasprintf(const char *fmt, va_list adx)
947 va_copy(adx_copy, adx);
948 VERIFY(vasprintf(&buf, fmt, adx_copy) != -1);
955 kmem_asprintf(const char *fmt, ...)
961 VERIFY(vasprintf(&buf, fmt, adx) != -1);
969 zfs_onexit_fd_hold(int fd, minor_t *minorp)
977 zfs_onexit_fd_rele(int fd)
983 zfs_onexit_add_cb(minor_t minor, void (*func)(void *), void *data,
984 uint64_t *action_handle)
990 spl_fstrans_mark(void)
992 return ((fstrans_cookie_t)0);
996 spl_fstrans_unmark(fstrans_cookie_t cookie)
1001 __spl_pf_fstrans_check(void)
1007 kmem_cache_reap_active(void)
1012 void *zvol_tag = "zvol_tag";
1015 zvol_create_minor(const char *name)
1020 zvol_create_minors_recursive(const char *name)
1025 zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
1030 zvol_rename_minors(spa_t *spa, const char *oldname, const char *newname,
1038 * path - fully qualified path to file
1039 * flags - file attributes O_READ / O_WRITE / O_EXCL
1040 * fpp - pointer to return file pointer
1042 * Returns 0 on success underlying error on failure.
1045 zfs_file_open(const char *path, int flags, int mode, zfs_file_t **fpp)
1054 if (!(flags & O_CREAT) && stat64(path, &st) == -1)
1057 if (!(flags & O_CREAT) && S_ISBLK(st.st_mode))
1060 if (flags & O_CREAT)
1061 old_umask = umask(0);
1063 fd = open64(path, flags, mode);
1067 if (flags & O_CREAT)
1068 (void) umask(old_umask);
1070 if (vn_dumpdir != NULL) {
1071 char *dumppath = umem_zalloc(MAXPATHLEN, UMEM_NOFAIL);
1072 char *inpath = basename((char *)(uintptr_t)path);
1074 (void) snprintf(dumppath, MAXPATHLEN,
1075 "%s/%s", vn_dumpdir, inpath);
1076 dump_fd = open64(dumppath, O_CREAT | O_WRONLY, 0666);
1077 umem_free(dumppath, MAXPATHLEN);
1078 if (dump_fd == -1) {
1087 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
1089 fp = umem_zalloc(sizeof (zfs_file_t), UMEM_NOFAIL);
1091 fp->f_dump_fd = dump_fd;
1098 zfs_file_close(zfs_file_t *fp)
1101 if (fp->f_dump_fd != -1)
1102 close(fp->f_dump_fd);
1104 umem_free(fp, sizeof (zfs_file_t));
1108 * Stateful write - use os internal file pointer to determine where to
1109 * write and update on successful completion.
1111 * fp - pointer to file (pipe, socket, etc) to write to
1112 * buf - buffer to write
1113 * count - # of bytes to write
1114 * resid - pointer to count of unwritten bytes (if short write)
1116 * Returns 0 on success errno on failure.
1119 zfs_file_write(zfs_file_t *fp, const void *buf, size_t count, ssize_t *resid)
1123 rc = write(fp->f_fd, buf, count);
1128 *resid = count - rc;
1129 } else if (rc != count) {
1137 * Stateless write - os internal file pointer is not updated.
1139 * fp - pointer to file (pipe, socket, etc) to write to
1140 * buf - buffer to write
1141 * count - # of bytes to write
1142 * off - file offset to write to (only valid for seekable types)
1143 * resid - pointer to count of unwritten bytes
1145 * Returns 0 on success errno on failure.
1148 zfs_file_pwrite(zfs_file_t *fp, const void *buf,
1149 size_t count, loff_t pos, ssize_t *resid)
1151 ssize_t rc, split, done;
1155 * To simulate partial disk writes, we split writes into two
1156 * system calls so that the process can be killed in between.
1157 * This is used by ztest to simulate realistic failure modes.
1159 sectors = count >> SPA_MINBLOCKSHIFT;
1160 split = (sectors > 0 ? rand() % sectors : 0) << SPA_MINBLOCKSHIFT;
1161 rc = pwrite64(fp->f_fd, buf, split, pos);
1164 rc = pwrite64(fp->f_fd, (char *)buf + split,
1165 count - split, pos + split);
1168 if (rc == -1 && errno == EINVAL) {
1170 * Under Linux, this most likely means an alignment issue
1171 * (memory or disk) due to O_DIRECT, so we abort() in order
1172 * to catch the offender.
1184 *resid = count - done;
1185 } else if (done != count) {
1193 * Stateful read - use os internal file pointer to determine where to
1194 * read and update on successful completion.
1196 * fp - pointer to file (pipe, socket, etc) to read from
1197 * buf - buffer to write
1198 * count - # of bytes to read
1199 * resid - pointer to count of unread bytes (if short read)
1201 * Returns 0 on success errno on failure.
1204 zfs_file_read(zfs_file_t *fp, void *buf, size_t count, ssize_t *resid)
1208 rc = read(fp->f_fd, buf, count);
1213 *resid = count - rc;
1214 } else if (rc != count) {
1222 * Stateless read - os internal file pointer is not updated.
1224 * fp - pointer to file (pipe, socket, etc) to read from
1225 * buf - buffer to write
1226 * count - # of bytes to write
1227 * off - file offset to read from (only valid for seekable types)
1228 * resid - pointer to count of unwritten bytes (if short write)
1230 * Returns 0 on success errno on failure.
1233 zfs_file_pread(zfs_file_t *fp, void *buf, size_t count, loff_t off,
1238 rc = pread64(fp->f_fd, buf, count, off);
1242 * Under Linux, this most likely means an alignment issue
1243 * (memory or disk) due to O_DIRECT, so we abort() in order to
1244 * catch the offender.
1246 if (errno == EINVAL)
1252 if (fp->f_dump_fd != -1) {
1255 status = pwrite64(fp->f_dump_fd, buf, rc, off);
1256 ASSERT(status != -1);
1260 *resid = count - rc;
1261 } else if (rc != count) {
1269 * lseek - set / get file pointer
1271 * fp - pointer to file (pipe, socket, etc) to read from
1272 * offp - value to seek to, returns current value plus passed offset
1273 * whence - see man pages for standard lseek whence values
1275 * Returns 0 on success errno on failure (ESPIPE for non seekable types)
1278 zfs_file_seek(zfs_file_t *fp, loff_t *offp, int whence)
1282 rc = lseek(fp->f_fd, *offp, whence);
1292 * Get file attributes
1294 * filp - file pointer
1295 * zfattr - pointer to file attr structure
1297 * Currently only used for fetching size and file mode
1299 * Returns 0 on success or error code of underlying getattr call on failure.
1302 zfs_file_getattr(zfs_file_t *fp, zfs_file_attr_t *zfattr)
1306 if (fstat64_blk(fp->f_fd, &st) == -1)
1309 zfattr->zfa_size = st.st_size;
1310 zfattr->zfa_mode = st.st_mode;
1318 * filp - file pointer
1319 * flags - O_SYNC and or O_DSYNC
1321 * Returns 0 on success or error code of underlying sync call on failure.
1324 zfs_file_fsync(zfs_file_t *fp, int flags)
1328 rc = fsync(fp->f_fd);
1336 * fallocate - allocate or free space on disk
1339 * mode (non-standard options for hole punching etc)
1340 * offset - offset to start allocating or freeing from
1341 * len - length to free / allocate
1346 zfs_file_fallocate(zfs_file_t *fp, int mode, loff_t offset, loff_t len)
1349 return (fallocate(fp->f_fd, mode, offset, len));
1351 return (EOPNOTSUPP);
1356 * Request current file pointer offset
1358 * fp - pointer to file
1360 * Returns current file offset.
1363 zfs_file_off(zfs_file_t *fp)
1365 return (lseek(fp->f_fd, SEEK_CUR, 0));
1371 * path - fully qualified file path
1373 * Returns 0 on success.
1378 zfs_file_unlink(const char *path)
1380 return (remove(path));
1384 * Get reference to file pointer
1386 * fd - input file descriptor
1387 * fpp - pointer to file pointer
1389 * Returns 0 on success EBADF on failure.
1390 * Unsupported in user space.
1393 zfs_file_get(int fd, zfs_file_t **fpp)
1397 return (EOPNOTSUPP);
1401 * Drop reference to file pointer
1403 * fd - input file descriptor
1405 * Unsupported in user space.
1408 zfs_file_put(int fd)
1414 zfsvfs_update_fromname(const char *oldname, const char *newname)