root/usr/src/lib/libzpool/common/sys/zfs_context.h
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
 * Copyright 2019 Joyent, Inc.
 * Copyright 2017 RackTop Systems.
 */

#ifndef _SYS_ZFS_CONTEXT_H
#define _SYS_ZFS_CONTEXT_H

#ifdef  __cplusplus
extern "C" {
#endif

#define _SYNCH_H

#define _SYS_VNODE_H
#define _SYS_VFS_H
#define _SYS_CALLB_H

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <thread.h>
#include <assert.h>
#include <alloca.h>
#include <umem.h>
#include <limits.h>
#include <atomic.h>
#include <dirent.h>
#include <time.h>
#include <procfs.h>
#include <pthread.h>
#include <setjmp.h>
#include <sys/debug.h>
#include <libsysevent.h>
#include <sys/note.h>
#include <sys/types.h>
#include <sys/cred.h>
#include <sys/sysmacros.h>
#include <sys/bitmap.h>
#include <sys/resource.h>
#include <sys/byteorder.h>
#include <sys/list.h>
#include <sys/uio.h>
#include <sys/zfs_debug.h>
#include <sys/sdt.h>
#include <sys/kstat.h>
#include <sys/u8_textprep.h>
#include <sys/sysevent/eventdefs.h>
#include <sys/sysevent/dev.h>
#include <sys/debug.h>
#include <sys/taskq.h>
#include <sys/taskq_impl.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/condvar.h>
#include <sys/cmn_err.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/random.h>
#include <sys/buf.h>
#include <sys/sid.h>
#include <sys/acl.h>
#include <sys/bitmap.h>
#include <sys/systeminfo.h>
#include <sys/cpuvar.h>
#include <sys/pset.h>
#include <sys/kobj.h>
#include <sys/fm/util.h>
#include "zfs.h"

/*
 * ZFS debugging
 */

#ifdef ZFS_DEBUG
extern void dprintf_setup(int *argc, char **argv);
#endif /* ZFS_DEBUG */

/*
 * DTrace SDT probes have different signatures in userland than they do in
 * the kernel.  If they're being used in kernel code, re-define them out of
 * existence for their counterparts in libzpool.
 *
 * Here's an example of how to use the set-error probes in userland:
 * zfs$target:::set-error /arg0 == EBUSY/ {stack();}
 *
 * Here's an example of how to use DTRACE_PROBE probes in userland:
 * If there is a probe declared as follows:
 * DTRACE_PROBE2(zfs__probe_name, uint64_t, blkid, dnode_t *, dn);
 * Then you can use it as follows:
 * zfs$target:::probe2 /copyinstr(arg0) == "zfs__probe_name"/
 *     {printf("%u %p\n", arg1, arg2);}
 */

#ifdef DTRACE_PROBE
#undef  DTRACE_PROBE
#endif  /* DTRACE_PROBE */
#define DTRACE_PROBE(a) \
        ZFS_PROBE0(#a)

#ifdef DTRACE_PROBE1
#undef  DTRACE_PROBE1
#endif  /* DTRACE_PROBE1 */
#define DTRACE_PROBE1(a, b, c) \
        ZFS_PROBE1(#a, (unsigned long)c)

#ifdef DTRACE_PROBE2
#undef  DTRACE_PROBE2
#endif  /* DTRACE_PROBE2 */
#define DTRACE_PROBE2(a, b, c, d, e) \
        ZFS_PROBE2(#a, (unsigned long)c, (unsigned long)e)

#ifdef DTRACE_PROBE3
#undef  DTRACE_PROBE3
#endif  /* DTRACE_PROBE3 */
#define DTRACE_PROBE3(a, b, c, d, e, f, g) \
        ZFS_PROBE3(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g)

#ifdef DTRACE_PROBE4
#undef  DTRACE_PROBE4
#endif  /* DTRACE_PROBE4 */
#define DTRACE_PROBE4(a, b, c, d, e, f, g, h, i) \
        ZFS_PROBE4(#a, (unsigned long)c, (unsigned long)e, (unsigned long)g, \
        (unsigned long)i)

/*
 * We use the comma operator so that this macro can be used without much
 * additional code.  For example, "return (EINVAL);" becomes
 * "return (SET_ERROR(EINVAL));".  Note that the argument will be evaluated
 * twice, so it should not have side effects (e.g. something like:
 * "return (SET_ERROR(log_error(EINVAL, info)));" would log the error twice).
 */
#define SET_ERROR(err) (ZFS_SET_ERROR(err), err)

/*
 * Threads
 */
#define kpreempt(x)     yield()
#define newproc(f, a, cid, pri, ctp, pid)       (ENOSYS)

/*
 * vnodes
 */
#define XVA_MAPSIZE     3
#define XVA_MAGIC       0x78766174

typedef struct vnode {
        uint64_t        v_size;
        int             v_fd;
        char            *v_path;
        int             v_dump_fd;
} vnode_t;

extern char *vn_dumpdir;
#define AV_SCANSTAMP_SZ 32              /* length of anti-virus scanstamp */

typedef struct xoptattr {
        timestruc_t     xoa_createtime; /* Create time of file */
        uint8_t         xoa_archive;
        uint8_t         xoa_system;
        uint8_t         xoa_readonly;
        uint8_t         xoa_hidden;
        uint8_t         xoa_nounlink;
        uint8_t         xoa_immutable;
        uint8_t         xoa_appendonly;
        uint8_t         xoa_nodump;
        uint8_t         xoa_settable;
        uint8_t         xoa_opaque;
        uint8_t         xoa_av_quarantined;
        uint8_t         xoa_av_modified;
        uint8_t         xoa_av_scanstamp[AV_SCANSTAMP_SZ];
        uint8_t         xoa_reparse;
        uint8_t         xoa_offline;
        uint8_t         xoa_sparse;
} xoptattr_t;

typedef struct vattr {
        uint_t          va_mask;        /* bit-mask of attributes */
        u_offset_t      va_size;        /* file size in bytes */
} vattr_t;


typedef struct xvattr {
        vattr_t         xva_vattr;      /* Embedded vattr structure */
        uint32_t        xva_magic;      /* Magic Number */
        uint32_t        xva_mapsize;    /* Size of attr bitmap (32-bit words) */
        uint32_t        *xva_rtnattrmapp;       /* Ptr to xva_rtnattrmap[] */
        uint32_t        xva_reqattrmap[XVA_MAPSIZE];    /* Requested attrs */
        uint32_t        xva_rtnattrmap[XVA_MAPSIZE];    /* Returned attrs */
        xoptattr_t      xva_xoptattrs;  /* Optional attributes */
} xvattr_t;

typedef struct vsecattr {
        uint_t          vsa_mask;       /* See below */
        int             vsa_aclcnt;     /* ACL entry count */
        void            *vsa_aclentp;   /* pointer to ACL entries */
        int             vsa_dfaclcnt;   /* default ACL entry count */
        void            *vsa_dfaclentp; /* pointer to default ACL entries */
        size_t          vsa_aclentsz;   /* ACE size in bytes of vsa_aclentp */
} vsecattr_t;

#define AT_TYPE         0x00001
#define AT_MODE         0x00002
#define AT_UID          0x00004
#define AT_GID          0x00008
#define AT_FSID         0x00010
#define AT_NODEID       0x00020
#define AT_NLINK        0x00040
#define AT_SIZE         0x00080
#define AT_ATIME        0x00100
#define AT_MTIME        0x00200
#define AT_CTIME        0x00400
#define AT_RDEV         0x00800
#define AT_BLKSIZE      0x01000
#define AT_NBLOCKS      0x02000
#define AT_SEQ          0x08000
#define AT_XVATTR       0x10000

#define CRCREAT         0

extern int fop_getattr(vnode_t *vp, vattr_t *vap);

#define VOP_CLOSE(vp, f, c, o, cr, ct)  0
#define VOP_PUTPAGE(vp, of, sz, fl, cr, ct)     0
#define VOP_GETATTR(vp, vap, fl, cr, ct)  fop_getattr((vp), (vap));
#define VOP_SPACE(vp, cmd, a, f, o, cr, ct)  0

#define VOP_FSYNC(vp, f, cr, ct)        fsync((vp)->v_fd)

#define VN_RELE(vp)     vn_close(vp)

extern int vn_open(char *path, int x1, int oflags, int mode, vnode_t **vpp,
    int x2, int x3);
extern int vn_openat(char *path, int x1, int oflags, int mode, vnode_t **vpp,
    int x2, int x3, vnode_t *vp, int fd);
extern int vn_rdwr(int uio, vnode_t *vp, void *addr, ssize_t len,
    offset_t offset, int x1, int x2, rlim64_t x3, void *x4, ssize_t *residp);
extern void vn_close(vnode_t *vp);

#define vn_remove(path, x1, x2)         remove(path)
#define vn_rename(from, to, seg)        rename((from), (to))
#define vn_is_readonly(vp)              B_FALSE

extern vnode_t *rootdir;

#include <sys/file.h>           /* for FREAD, FWRITE, etc */
#include <sys/sunddi.h>         /* for ddi_strtoul, ddi_strtoull, etc */
#include <sys/cyclic.h>         /* for cyclic_add, cyclic remove, etc */
#include <vm/seg_kmem.h>        /* for zio_arena */

/*
 * Random stuff
 */
#define max_ncpus       64
#define boot_ncpus      (sysconf(_SC_NPROCESSORS_ONLN))

#define minclsyspri     60
#define maxclsyspri     99

#if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
#define _zfs_expect(expr, value)    (__builtin_expect((expr), (value)))
#else
#define _zfs_expect(expr, value)    (expr)
#endif

#define likely(x)       _zfs_expect((x) != 0, 1)
#define unlikely(x)     _zfs_expect((x) != 0, 0)

#define CPU_SEQID       (thr_self() & (max_ncpus - 1))

extern void kernel_init(int);
extern void kernel_fini(void);

struct spa;
extern void show_pool_stats(struct spa *);
extern int set_global_var(char *arg);

typedef struct callb_cpr {
        kmutex_t        *cc_lockp;
} callb_cpr_t;

#define CALLB_CPR_INIT(cp, lockp, func, name)   {               \
        (cp)->cc_lockp = lockp;                                 \
}

#define CALLB_CPR_SAFE_BEGIN(cp) {                              \
        ASSERT(MUTEX_HELD((cp)->cc_lockp));                     \
}

#define CALLB_CPR_SAFE_END(cp, lockp) {                         \
        ASSERT(MUTEX_HELD((cp)->cc_lockp));                     \
}

#define CALLB_CPR_EXIT(cp) {                                    \
        ASSERT(MUTEX_HELD((cp)->cc_lockp));                     \
        mutex_exit((cp)->cc_lockp);                             \
}

#define zone_dataset_visible(x, y)      (1)
#define INGLOBALZONE(z)                 (1)
extern uint32_t zone_get_hostid(void *zonep);

/*
 * In ZoL the following defines were added to their sys/avl.h header, but
 * we want to limit these to the ZFS code on illumos.
 */
#define TREE_ISIGN(a)   (((a) > 0) - ((a) < 0))
#define TREE_CMP(a, b)  (((a) > (b)) - ((a) < (b)))
#define TREE_PCMP(a, b) \
        (((uintptr_t)(a) > (uintptr_t)(b)) - ((uintptr_t)(a) < (uintptr_t)(b)))

extern int zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr);
extern int zfs_secpolicy_rename_perms(const char *from, const char *to,
    cred_t *cr);
extern int zfs_secpolicy_destroy_perms(const char *name, cred_t *cr);

#define ddi_log_sysevent(_a, _b, _c, _d, _e, _f, _g) \
        sysevent_post_event(_c, _d, _b, "libzpool", _e, _f)

#ifdef  __cplusplus
}
#endif

#endif  /* _SYS_ZFS_CONTEXT_H */