1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
  14  */
  15 
  16 #ifndef _KRRP_AUTOSNAP_H
  17 #define _KRRP_AUTOSNAP_H
  18 
  19 #include <sys/sysmacros.h>
  20 #include <sys/kmem.h>
  21 #include <sys/atomic.h>
  22 #include <sys/stream.h>
  23 #include <sys/list.h>
  24 #include <sys/modctl.h>
  25 #include <sys/class.h>
  26 #include <sys/cmn_err.h>
  27 
  28 #include <sys/autosnap.h>
  29 
  30 #include <krrp_error.h>
  31 
  32 #include "krrp_queue.h"
  33 
  34 #ifdef __cplusplus
  35 extern "C" {
  36 #endif
  37 
  38 #define krrp_autosnap_lock(a)                   mutex_enter(&(a)->mtx)
  39 #define krrp_autosnap_unlock(a)                 mutex_exit(&(a)->mtx)
  40 #define krrp_autosnap_cv_wait(a)                cv_wait(&(a)->cv, &(a)->mtx)
  41 #define krrp_autosnap_cv_signal(a)              cv_signal(&(a)->cv)
  42 #define krrp_autosnap_cv_broadcast(a)   cv_broadcast(&(a)->cv)
  43 
  44 typedef void krrp_autosnap_restore_cb_t(void *, const char *, uint64_t);
  45 
  46 /*
  47  *            |
  48  *            | instance creation
  49  *            v
  50  * +-----------------------------+
  51  * | KRRP_AUTOSNAP_STATE_UNKNOWN |
  52  * +-----------------------------+
  53  *            |
  54  *            | instance initialized
  55  *            v
  56  * +-----------------------------+
  57  * | KRRP_AUTOSNAP_STATE_CREATED |<-
  58  * +-----------------------------+ |
  59  *            |                    |
  60  *            | register           | instance destroying
  61  *            | zfs-autosnap       |
  62  *            | handler            |
  63  *            v                    |
  64  * +--------------------------------+
  65  * | KRRP_AUTOSNAP_STATE_REGISTERED |<--
  66  * +--------------------------------+  |
  67  *            |                        |
  68  *            | start the related      | stop the related
  69  *            | replication session    | replication session
  70  *            v                        |
  71  * +----------------------------+      |
  72  * | KRRP_AUTOSNAP_STATE_ACTIVE |-------
  73  * +----------------------------+
  74  *
  75  *
  76  * KRRP_AUTOSNAP_STATE_UNKNOWN
  77  *    instance structure has been allocated
  78  *
  79  * KRRP_AUTOSNAP_STATE_CREATED
  80  *    instance structure has been initialized
  81  *
  82  * KRRP_AUTOSNAP_STATE_REGISTERED
  83  *    instance zfs-autosnap handler and
  84  *    required callbacks have been registered
  85  *
  86  * KRRP_AUTOSNAP_STATE_ACTIVE
  87  *    instance structure is ready to use
  88  */
  89 typedef enum {
  90         KRRP_AUTOSNAP_STATE_UNKNOWN = 0,
  91         KRRP_AUTOSNAP_STATE_CREATED,
  92         KRRP_AUTOSNAP_STATE_REGISTERED,
  93         KRRP_AUTOSNAP_STATE_ACTIVE
  94 } krrp_autosnap_state_t;
  95 
  96 typedef struct krrp_autonap_s {
  97         const char                              *dataset;
  98         kmutex_t                                mtx;
  99         kcondvar_t                              cv;
 100         krrp_autosnap_state_t   state;
 101         size_t                                  ref_cnt;
 102         void                                    *zfs_ctx;
 103         krrp_queue_t                    *txg_to_rele;
 104         size_t                                  keep_snaps;
 105         autosnap_flags_t                flags;
 106 } krrp_autosnap_t;
 107 
 108 void krrp_autosnap_rside_create(krrp_autosnap_t **result_autosnap,
 109     size_t keep_snaps, const char *dataset, boolean_t recursive);
 110 void krrp_autosnap_wside_create(krrp_autosnap_t **result_autosnap,
 111     size_t keep_snaps, const char *dataset);
 112 void krrp_autosnap_destroy(krrp_autosnap_t *autosnap);
 113 
 114 boolean_t krrp_autosnap_try_hold_to_confirm(krrp_autosnap_t *autosnap);
 115 void krrp_autosnap_unhold(krrp_autosnap_t *autosnap);
 116 
 117 int krrp_autosnap_activate(krrp_autosnap_t *autosnap, uint64_t incr_snap_txg,
 118     autosnap_confirm_cb confirm_cb,
 119     autosnap_notify_created_cb notify_cb,
 120     autosnap_error_cb error_cb,
 121     krrp_autosnap_restore_cb_t restore_cb, void *cb_arg,
 122     krrp_error_t *error);
 123 void krrp_autosnap_deactivate(krrp_autosnap_t *autosnap);
 124 
 125 void krrp_autosnap_create_snapshot(krrp_autosnap_t *autosnap);
 126 
 127 void krrp_autosnap_txg_rele(krrp_autosnap_t *, uint64_t, uint64_t);
 128 void krrp_autosnap_txg_rele_one(krrp_autosnap_t *, uint64_t);
 129 
 130 #ifdef __cplusplus
 131 }
 132 #endif
 133 
 134 #endif /* _KRRP_AUTOSNAP_H */