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 /* Copyright 2017 Nexenta Systems, Inc. All rights reserved. */
45
46 #ifndef _TLM_BUFFERS_H_
47 #define _TLM_BUFFERS_H_
48
49 #include <sys/types.h>
50 #include <stdlib.h>
51 #include <limits.h>
52 #include <sys/stat.h>
53 #include <thread.h>
54 #include "tlm.h"
55
56 #ifndef RECORDSIZE
57 #define RECORDSIZE 512
58 #endif /* !RECORDSIZE */
59
60 #define DOTDOT_DIR ".."
61 #define IS_DOTDOT(s) (strcmp(s, DOTDOT_DIR) == 0)
62 #define SLASH '/'
63
64 #define NDMP_MAX_SELECTIONS 64
65
66 /*
67 * List of files/directories to be excluded from backup list.
68 */
69 #define EXCL_PROC "/proc"
70 #define EXCL_TMP "/tmp"
71
72
73 typedef struct tlm_buffer {
74 char *tb_buffer_data; /* area to be used for I/O */
75 long tb_buffer_size; /* number of valid bytes in the buffer */
76 long tb_buffer_spot; /* current location in the I/O buffer */
77 longlong_t tb_seek_spot; /* for BACKUP */
78 /* where in the file this buffer stops. */
79 /* this is used for the Multi Volume */
80 /* Header record. */
81 longlong_t tb_file_size; /* for BACKUP */
82 /* how much of the file is left. */
83 long tb_full : 1,
84 tb_eot : 1,
85 tb_eof : 1;
86 int tb_errno; /* I/O error values */
87 } tlm_buffer_t;
88
89
90 /*
91 * Flags for tlm_buffers.
92 */
93 #define TLM_BUF_IN_READY 0x00000001
94 #define TLM_BUF_OUT_READY 0x00000002
95
96 typedef struct tlm_buffers {
97 int tbs_ref; /* number of threads using this */
98 short tbs_buffer_in; /* buffer to be filled */
99 short tbs_buffer_out; /* buffer to be emptied */
100 /* these are indexes into tlm_buffers */
101 mutex_t tbs_mtx;
102 cond_t tbs_in_cv;
103 cond_t tbs_out_cv;
104 uint32_t tbs_flags;
105 long tbs_data_transfer_size; /* max size of read/write buffer */
106 longlong_t tbs_offset;
107 tlm_buffer_t tbs_buffer[TLM_TAPE_BUFFERS];
108 } tlm_buffers_t;
109
110 typedef struct tlm_cmd {
111 int tc_ref; /* number of threads using this */
112 mutex_t tc_mtx;
113 cond_t tc_cv;
114 uint32_t tc_flags;
115 int tc_reader; /* writer to reader */
116 int tc_writer; /* reader to writer */
117 char tc_file_name[TLM_MAX_PATH_NAME]; /* name of last file */
118 /* for restore */
119 tlm_buffers_t *tc_buffers; /* reader-writer speedup buffers */
120 } tlm_cmd_t;
121
122 typedef struct tlm_commands {
123 int tcs_reader; /* commands to all readers */
124 int tcs_writer; /* commands to all writers */
125 int tcs_reader_count; /* number of active readers */
126 int tcs_writer_count; /* number of active writers */
127 int tcs_error; /* worker errors */
128 char tcs_message[TLM_LINE_SIZE]; /* worker message back to user */
129 tlm_cmd_t *tcs_command; /* IPC area between read-write */
130 } tlm_commands_t;
131
132
133 typedef struct tlm_job_stats {
134 char js_job_name[TLM_MAX_BACKUP_JOB_NAME];
135 longlong_t js_bytes_total; /* tape bytes in or out so far */
136 longlong_t js_bytes_in_file; /* remaining data in a file */
137 longlong_t js_files_so_far; /* files backed up so far */
138 longlong_t js_files_total; /* number of files to be backed up */
139 int js_errors;
140 time_t js_start_time; /* start time (GMT time) */
141 time_t js_start_ltime; /* start time (local time) */
142 time_t js_stop_time; /* stop time (local time) */
143 time_t js_chkpnt_time; /* checkpoint creation (GMT time) */
144 void *js_callbacks;
145 } tlm_job_stats_t;
146
147
148 struct full_dir_info {
149 fs_fhandle_t fd_dir_fh;
150 char fd_dir_name[TLM_MAX_PATH_NAME];
151 };
152
153 /*
154 * For more info please refer to
155 * "Functional Specification Document: Usgin new LBR engine in NDMP",
156 * Revision: 0.2
157 * Document No.: 101438.
158 * the "File history of backup" section
159 */
160 typedef struct lbr_fhlog_call_backs {
161 void *fh_cookie;
162 int (*fh_logpname)();
163 int (*fh_log_dir)();
164 int (*fh_log_node)();
165 } lbr_fhlog_call_backs_t;
166
167
168 typedef struct bk_selector {
169 void *bs_cookie;
170 int bs_level;
171 int bs_ldate;
172 boolean_t (*bs_fn)(struct bk_selector *bks, struct stat64 *s);
173 } bk_selector_t;
174
175
176 /*
177 * Call back structure to create new name for objects at restore time.
178 */
179 struct rs_name_maker;
180 typedef char *(*rsm_fp_t)(struct rs_name_maker *,
181 char *buf,
182 int pos,
183 char *path);
184
185 struct rs_name_maker {
186 rsm_fp_t rn_fp;
187 void *rn_nlp;
188 };
189
190
191 /*
192 * RSFLG_OVR_*: overwriting policies. Refer to LBR FSD for more info.
193 * RSFLG_MATCH_WCARD: should wildcards be supported in the selection list.
194 * RSFLG_IGNORE_CASE: should the compare be case-insensetive. NDMP needs
195 * case-sensetive name comparison.
196 */
197 #define RSFLG_OVR_ALWAYS 0x00000001
198 #define RSFLG_OVR_NEVER 0x00000002
199 #define RSFLG_OVR_UPDATE 0x00000004
200 #define RSFLG_MATCH_WCARD 0x00000008
201 #define RSFLG_IGNORE_CASE 0x00000010
202
203
204 /*
205 * Different cases where two paths can match with each other.
206 * Parent means that the current path, is parent of an entry in
207 * the selection list.
208 * Child means that the current path, is child of an entry in the
209 * selection list.
210 */
211 #define PM_NONE 0
212 #define PM_EXACT 1
213 #define PM_PARENT 2
214 #define PM_CHILD 3
215
216 extern tlm_job_stats_t *tlm_new_job_stats(char *);
217 extern tlm_job_stats_t *tlm_ref_job_stats(char *);
218 extern void tlm_un_ref_job_stats(char *);
219 extern boolean_t tlm_is_excluded(char *, char *, char **);
220 extern char *tlm_build_snapshot_name(char *, char *, char *);
221 extern char *tlm_remove_checkpoint(char *, char *);
222 extern tlm_buffers_t *tlm_allocate_buffers(boolean_t, long);
223 extern tlm_buffer_t *tlm_buffer_advance_in_idx(tlm_buffers_t *);
224 extern tlm_buffer_t *tlm_buffer_advance_out_idx(tlm_buffers_t *);
225 extern tlm_buffer_t *tlm_buffer_in_buf(tlm_buffers_t *, int *);
226 extern tlm_buffer_t *tlm_buffer_out_buf(tlm_buffers_t *, int *);
227 extern void tlm_buffer_mark_empty(tlm_buffer_t *);
228 extern void tlm_buffer_release_in_buf(tlm_buffers_t *);
229 extern void tlm_buffer_release_out_buf(tlm_buffers_t *);
230 extern void tlm_buffer_in_buf_wait(tlm_buffers_t *);
231 extern void tlm_buffer_out_buf_wait(tlm_buffers_t *);
232 extern void tlm_buffer_in_buf_timed_wait(tlm_buffers_t *, unsigned);
233 extern void tlm_buffer_out_buf_timed_wait(tlm_buffers_t *, unsigned);
234 extern char *tlm_get_write_buffer(long, long *, tlm_buffers_t *, int);
235 extern char *tlm_get_read_buffer(int, int *, tlm_buffers_t *, int *);
236 extern void tlm_unget_read_buffer(tlm_buffers_t *, int);
237 extern void tlm_unget_write_buffer(tlm_buffers_t *, int);
238 extern void tlm_release_buffers(tlm_buffers_t *);
239 extern tlm_cmd_t *tlm_create_reader_writer_ipc(boolean_t, long);
240 extern void tlm_release_reader_writer_ipc(tlm_cmd_t *);
241
242 extern void tlm_cmd_wait(tlm_cmd_t *, uint32_t);
243 extern void tlm_cmd_signal(tlm_cmd_t *, uint32_t);
244
245 typedef int (*path_hist_func_t)(lbr_fhlog_call_backs_t *,
246 char *,
247 struct stat64 *,
248 u_longlong_t);
249
250 typedef int (*dir_hist_func_t)(lbr_fhlog_call_backs_t *,
251 char *,
252 struct stat64 *);
253
254 typedef int (*node_hist_func_t)(lbr_fhlog_call_backs_t *,
255 char *,
256 char *,
257 struct stat64 *,
258 u_longlong_t);
259
260 lbr_fhlog_call_backs_t *lbrlog_callbacks_init(void *,
261 path_hist_func_t,
262 dir_hist_func_t,
263 node_hist_func_t);
264
265 typedef struct {
266 tlm_commands_t *ba_commands;
267 tlm_cmd_t *ba_cmd;
268 char *ba_job;
269 char *ba_dir;
270 char *ba_sels[NDMP_MAX_SELECTIONS];
271 int ba_count;
272 pthread_barrier_t ba_barrier;
273 } tlm_backup_restore_arg_t;
274
275 extern void lbrlog_callbacks_done(lbr_fhlog_call_backs_t *);
276
277 extern boolean_t tlm_cat_path(char *, char *, char *);
278
279 extern char *trim_name(char *);
280
281 extern struct full_dir_info *dup_dir_info(struct full_dir_info *);
282 extern void write_tar_eof(tlm_cmd_t *);
283 extern int tlm_get_chkpnt_time(char *, time_t *);
284 extern struct full_dir_info *tlm_new_dir_info(fs_fhandle_t *,
285 char *,
286 char *);
287 extern void tlm_release_list(char **);
288 extern longlong_t tlm_get_data_offset(tlm_cmd_t *);
289 extern int tlm_tarhdr_size(void);
290
291 #endif /* _TLM_BUFFERS_H_ */