1 /*
   2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
   3  * Use is subject to license terms.
   4  */
   5 
   6 /*
   7  * BSD 3 Clause License
   8  *
   9  * Copyright (c) 2007, The Storage Networking Industry Association.
  10  *
  11  * Redistribution and use in source and binary forms, with or without
  12  * modification, are permitted provided that the following conditions
  13  * are met:
  14  *      - Redistributions of source code must retain the above copyright
  15  *        notice, this list of conditions and the following disclaimer.
  16  *
  17  *      - Redistributions in binary form must reproduce the above copyright
  18  *        notice, this list of conditions and the following disclaimer in
  19  *        the documentation and/or other materials provided with the
  20  *        distribution.
  21  *
  22  *      - Neither the name of The Storage Networking Industry Association (SNIA)
  23  *        nor the names of its contributors may be used to endorse or promote
  24  *        products derived from this software without specific prior written
  25  *        permission.
  26  *
  27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37  * POSSIBILITY OF SUCH DAMAGE.
  38  */
  39 /*
  40  * This defines structures used to pass information between threads
  41  * for both local-backup and NDMP.
  42  *
  43  */
  44 
  45 #ifndef _TLM_BUFFERS_H_
  46 #define _TLM_BUFFERS_H_
  47 
  48 #include <sys/types.h>
  49 #include <stdlib.h>
  50 #include <limits.h>
  51 #include <sys/stat.h>
  52 #include <thread.h>
  53 #include "tlm.h"
  54 
  55 #ifndef RECORDSIZE
  56 #define RECORDSIZE      512
  57 #endif /* !RECORDSIZE */
  58 
  59 #define DOTDOT_DIR      ".."
  60 #define IS_DOTDOT(s)    (strcmp(s, DOTDOT_DIR) == 0)
  61 #define SLASH   '/'
  62 
  63 #define NDMP_MAX_SELECTIONS     64
  64 
  65 /*
  66  * List of files/directories to be excluded from backup list.
  67  */
  68 #define EXCL_PROC       "/proc"
  69 #define EXCL_TMP        "/tmp"
  70 
  71 
  72 typedef struct  tlm_buffer {
  73         char    *tb_buffer_data;        /* area to be used for I/O */
  74         long    tb_buffer_size; /* number of valid bytes in the buffer */
  75         long    tb_buffer_spot; /* current location in the I/O buffer */
  76         longlong_t tb_seek_spot;        /* for BACKUP */
  77                                 /* where in the file this buffer stops. */
  78                                 /* this is used for the Multi Volume */
  79                                 /* Header record. */
  80         longlong_t tb_file_size;        /* for BACKUP */
  81                                         /* how much of the file is left. */
  82         long    tb_full : 1,
  83                 tb_eot  : 1,
  84                 tb_eof  : 1;
  85         int     tb_errno;       /* I/O error values */
  86 } tlm_buffer_t;
  87 
  88 
  89 /*
  90  * Flags for tlm_buffers.
  91  */
  92 #define TLM_BUF_IN_READY        0x00000001
  93 #define TLM_BUF_OUT_READY       0x00000002
  94 
  95 typedef struct  tlm_buffers {
  96         int     tbs_ref;        /* number of threads using this */
  97         short   tbs_buffer_in;  /* buffer to be filled */
  98         short   tbs_buffer_out; /* buffer to be emptied */
  99                                 /* these are indexes into tlm_buffers */
 100         mutex_t tbs_mtx;
 101         cond_t  tbs_in_cv;
 102         cond_t  tbs_out_cv;
 103         uint32_t        tbs_flags;
 104         long    tbs_data_transfer_size; /* max size of read/write buffer */
 105         longlong_t tbs_offset;
 106         tlm_buffer_t tbs_buffer[TLM_TAPE_BUFFERS];
 107 } tlm_buffers_t;
 108 
 109 typedef struct  tlm_cmd {
 110         int     tc_ref;                 /* number of threads using this */
 111         mutex_t tc_mtx;
 112         cond_t  tc_cv;
 113         uint32_t        tc_flags;
 114         int     tc_reader;              /* writer to reader */
 115         int     tc_writer;              /* reader to writer */
 116         char    tc_file_name[TLM_MAX_PATH_NAME]; /* name of last file */
 117                                                 /* for restore */
 118         tlm_buffers_t *tc_buffers; /* reader-writer speedup buffers */
 119 } tlm_cmd_t;
 120 
 121 typedef struct  tlm_commands {
 122         int     tcs_reader;     /* commands to all readers */
 123         int     tcs_writer;     /* commands to all writers */
 124         int     tcs_reader_count;       /* number of active readers */
 125         int     tcs_writer_count;       /* number of active writers */
 126         int     tcs_error;      /* worker errors */
 127         char    tcs_message[TLM_LINE_SIZE]; /* worker message back to user */
 128         tlm_cmd_t *tcs_command; /* IPC area between read-write */
 129 } tlm_commands_t;
 130 
 131 
 132 typedef struct  tlm_job_stats {
 133         char    js_job_name[TLM_MAX_BACKUP_JOB_NAME];
 134         longlong_t js_bytes_total;      /* tape bytes in or out so far */
 135         longlong_t js_bytes_in_file;    /* remaining data in a file */
 136         longlong_t js_files_so_far;     /* files backed up so far */
 137         longlong_t js_files_total;      /* number of files to be backed up */
 138         int     js_errors;
 139         time_t  js_start_time;          /* start time (GMT time) */
 140         time_t  js_start_ltime;         /* start time (local time) */
 141         time_t  js_stop_time;           /* stop time (local time) */
 142         time_t  js_chkpnt_time;         /* checkpoint creation (GMT time) */
 143         void    *js_callbacks;
 144 } tlm_job_stats_t;
 145 
 146 
 147 struct full_dir_info {
 148         fs_fhandle_t fd_dir_fh;
 149         char fd_dir_name[TLM_MAX_PATH_NAME];
 150 };
 151 
 152 /*
 153  * For more info please refer to
 154  * "Functional Specification Document: Usgin new LBR engine in NDMP",
 155  * Revision: 0.2
 156  * Document No.: 101438.
 157  * the "File history of backup" section
 158  */
 159 typedef struct lbr_fhlog_call_backs {
 160         void *fh_cookie;
 161         int (*fh_logpname)();
 162         int (*fh_log_dir)();
 163         int (*fh_log_node)();
 164 } lbr_fhlog_call_backs_t;
 165 
 166 
 167 typedef struct bk_selector {
 168         void *bs_cookie;
 169         int bs_level;
 170         int bs_ldate;
 171         boolean_t (*bs_fn)(struct bk_selector *bks, struct stat64 *s);
 172 } bk_selector_t;
 173 
 174 
 175 /*
 176  * Call back structure to create new name for objects at restore time.
 177  */
 178 struct rs_name_maker;
 179 typedef char *(*rsm_fp_t)(struct rs_name_maker *,
 180         char *buf,
 181         int pos,
 182         char *path);
 183 
 184 struct rs_name_maker {
 185         rsm_fp_t rn_fp;
 186         void *rn_nlp;
 187 };
 188 
 189 
 190 /*
 191  *  RSFLG_OVR_*: overwriting policies.  Refer to LBR FSD for more info.
 192  *  RSFLG_MATCH_WCARD: should wildcards be supported in the selection list.
 193  *  RSFLG_IGNORE_CASE: should the compare be case-insensetive.  NDMP needs
 194  *      case-sensetive name comparison.
 195  */
 196 #define RSFLG_OVR_ALWAYS        0x00000001
 197 #define RSFLG_OVR_NEVER         0x00000002
 198 #define RSFLG_OVR_UPDATE        0x00000004
 199 #define RSFLG_MATCH_WCARD       0x00000008
 200 #define RSFLG_IGNORE_CASE       0x00000010
 201 
 202 
 203 /*
 204  * Different cases where two paths can match with each other.
 205  * Parent means that the current path, is parent of an entry in
 206  * the selection list.
 207  * Child means that the current path, is child of an entry in the
 208  * selection list.
 209  */
 210 #define PM_NONE         0
 211 #define PM_EXACT        1
 212 #define PM_PARENT       2
 213 #define PM_CHILD        3
 214 
 215 extern tlm_job_stats_t *tlm_new_job_stats(char *);
 216 extern tlm_job_stats_t *tlm_ref_job_stats(char *);
 217 extern void tlm_un_ref_job_stats(char *);
 218 extern boolean_t tlm_is_excluded(char *, char *, char **);
 219 extern char *tlm_build_snapshot_name(char *, char *, char *);
 220 extern char *tlm_remove_checkpoint(char *, char *);
 221 extern tlm_buffers_t *tlm_allocate_buffers(boolean_t, long);
 222 extern tlm_buffer_t *tlm_buffer_advance_in_idx(tlm_buffers_t *);
 223 extern tlm_buffer_t *tlm_buffer_advance_out_idx(tlm_buffers_t *);
 224 extern tlm_buffer_t *tlm_buffer_in_buf(tlm_buffers_t *, int *);
 225 extern tlm_buffer_t *tlm_buffer_out_buf(tlm_buffers_t *, int *);
 226 extern void tlm_buffer_mark_empty(tlm_buffer_t *);
 227 extern void tlm_buffer_release_in_buf(tlm_buffers_t *);
 228 extern void tlm_buffer_release_out_buf(tlm_buffers_t *);
 229 extern void tlm_buffer_in_buf_wait(tlm_buffers_t *);
 230 extern void tlm_buffer_out_buf_wait(tlm_buffers_t *);
 231 extern void tlm_buffer_in_buf_timed_wait(tlm_buffers_t *, unsigned);
 232 extern void tlm_buffer_out_buf_timed_wait(tlm_buffers_t *, unsigned);
 233 extern char *tlm_get_write_buffer(long, long *, tlm_buffers_t *, int);
 234 extern char *tlm_get_read_buffer(int, int *, tlm_buffers_t *, int *);
 235 extern void tlm_unget_read_buffer(tlm_buffers_t *, int);
 236 extern void tlm_unget_write_buffer(tlm_buffers_t *, int);
 237 extern void tlm_release_buffers(tlm_buffers_t *);
 238 extern tlm_cmd_t *tlm_create_reader_writer_ipc(boolean_t, long);
 239 extern void tlm_release_reader_writer_ipc(tlm_cmd_t *);
 240 
 241 extern void tlm_cmd_wait(tlm_cmd_t *, uint32_t);
 242 extern void tlm_cmd_signal(tlm_cmd_t *, uint32_t);
 243 
 244 typedef int (*path_hist_func_t)(lbr_fhlog_call_backs_t *,
 245     char *,
 246     struct stat64 *,
 247     u_longlong_t);
 248 
 249 typedef int (*dir_hist_func_t)(lbr_fhlog_call_backs_t *,
 250     char *,
 251     struct stat64 *);
 252 
 253 typedef int (*node_hist_func_t)(lbr_fhlog_call_backs_t *,
 254     char *,
 255     char *,
 256     struct stat64 *,
 257     u_longlong_t);
 258 
 259 lbr_fhlog_call_backs_t *lbrlog_callbacks_init(void *,
 260     path_hist_func_t,
 261     dir_hist_func_t,
 262     node_hist_func_t);
 263 
 264 typedef struct {
 265         tlm_commands_t *ba_commands;
 266         tlm_cmd_t *ba_cmd;
 267         char *ba_job;
 268         char *ba_dir;
 269         char *ba_sels[NDMP_MAX_SELECTIONS];
 270         pthread_barrier_t ba_barrier;
 271 } tlm_backup_restore_arg_t;
 272 
 273 extern void lbrlog_callbacks_done(lbr_fhlog_call_backs_t *);
 274 
 275 extern boolean_t tlm_cat_path(char *, char *, char *);
 276 
 277 extern char *trim_name(char *);
 278 
 279 extern struct full_dir_info *dup_dir_info(struct full_dir_info *);
 280 extern void write_tar_eof(tlm_cmd_t *);
 281 extern int tlm_get_chkpnt_time(char *, int, time_t *, char *);
 282 extern struct full_dir_info *tlm_new_dir_info(fs_fhandle_t *,
 283     char *,
 284     char *);
 285 extern void tlm_release_list(char **);
 286 extern longlong_t tlm_get_data_offset(tlm_cmd_t *);
 287 extern int tlm_tarhdr_size(void);
 288 
 289 #endif  /* _TLM_BUFFERS_H_ */