xref: /titanic_50/usr/src/cmd/ndmpd/tlm/tlm_lib.c (revision cadbfdc3bdb156e92d7a88978bc98ea87f6e037f)
1 /*
2  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
3  */
4 
5 /*
6  * BSD 3 Clause License
7  *
8  * Copyright (c) 2007, The Storage Networking Industry Association.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 	- Redistributions of source code must retain the above copyright
14  *	  notice, this list of conditions and the following disclaimer.
15  *
16  * 	- Redistributions in binary form must reproduce the above copyright
17  *	  notice, this list of conditions and the following disclaimer in
18  *	  the documentation and/or other materials provided with the
19  *	  distribution.
20  *
21  *	- Neither the name of The Storage Networking Industry Association (SNIA)
22  *	  nor the names of its contributors may be used to endorse or promote
23  *	  products derived from this software without specific prior written
24  *	  permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 #include <sys/errno.h>
39 #include <ctype.h>
40 #include <stdlib.h>
41 #include <time.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include <libzfs.h>
45 #include <pthread.h>
46 #include "tlm.h"
47 #include "tlm_proto.h"
48 #include <sys/mtio.h>
49 #include <sys/mnttab.h>
50 #include <sys/mntent.h>
51 #include <sys/statvfs.h>
52 #include <sys/scsi/impl/uscsi.h>
53 #include <sys/scsi/scsi.h>
54 #include <sys/mtio.h>
55 #include <thread.h>
56 #include <synch.h>
57 #include <sys/mutex.h>
58 #include <sys/sysmacros.h>
59 #include <sys/mkdev.h>
60 
61 /*
62  * Tar archiving ops vector
63  */
64 tm_ops_t tm_tar_ops = {
65 	"tar",
66 	tar_putfile,
67 	tar_putdir,
68 	NULL,
69 	tar_getfile,
70 	tar_getdir,
71 	NULL
72 };
73 
74 extern	libzfs_handle_t *zlibh;
75 extern	mutex_t zlib_mtx;
76 
77 /*
78  * get the next tape buffer from the drive's pool of buffers
79  */
80 /*ARGSUSED*/
81 char *
82 tlm_get_write_buffer(long want, long *actual_size,
83     tlm_buffers_t *buffers, int zero)
84 {
85 	int	buf = buffers->tbs_buffer_in;
86 	tlm_buffer_t *buffer = &buffers->tbs_buffer[buf];
87 	int	align_size = RECORDSIZE - 1;
88 	char	*rec;
89 
90 	/*
91 	 * make sure the allocation is in chunks of 512 bytes
92 	 */
93 	want += align_size;
94 	want &= ~align_size;
95 
96 	*actual_size = buffer->tb_buffer_size - buffer->tb_buffer_spot;
97 	if (*actual_size <= 0) {
98 		/*
99 		 * no room, send this one
100 		 * and wait for a free one
101 		 */
102 		if (!buffer->tb_full) {
103 			/*
104 			 * we are now ready to send a full buffer
105 			 * instead of trying to get a new buffer
106 			 *
107 			 * do not send if we failed to get a buffer
108 			 * on the previous call
109 			 */
110 			buffer->tb_full = TRUE;
111 
112 			/*
113 			 * tell the writer that a buffer is available
114 			 */
115 			tlm_buffer_release_in_buf(buffers);
116 
117 			buffer = tlm_buffer_advance_in_idx(buffers);
118 		}
119 
120 		buffer = tlm_buffer_in_buf(buffers, NULL);
121 
122 		if (buffer->tb_full) {
123 			/*
124 			 * wait for the writer to free up a buffer
125 			 */
126 			tlm_buffer_out_buf_timed_wait(buffers, 500);
127 		}
128 
129 		buffer = tlm_buffer_in_buf(buffers, NULL);
130 		if (buffer->tb_full) {
131 			/*
132 			 * the next buffer is still full
133 			 * of data from previous activity
134 			 *
135 			 * nothing has changed.
136 			 */
137 			return (0);
138 		}
139 
140 		buffer->tb_buffer_spot = 0;
141 		*actual_size = buffer->tb_buffer_size - buffer->tb_buffer_spot;
142 	}
143 
144 	*actual_size = min(want, *actual_size);
145 	rec = &buffer->tb_buffer_data[buffer->tb_buffer_spot];
146 	buffer->tb_buffer_spot += *actual_size;
147 	buffers->tbs_offset += *actual_size;
148 	if (zero) {
149 		(void) memset(rec, 0, *actual_size);
150 	}
151 	return (rec);
152 }
153 
154 /*
155  * get a read record from the tape buffer,
156  * and read a tape block if necessary
157  */
158 /*ARGSUSED*/
159 char *
160 tlm_get_read_buffer(int want, int *error,
161     tlm_buffers_t *buffers, int *actual_size)
162 {
163 	tlm_buffer_t *buffer;
164 	int	align_size = RECORDSIZE - 1;
165 	int	buf;
166 	int	current_size;
167 	char	*rec;
168 
169 	buf = buffers->tbs_buffer_out;
170 	buffer = &buffers->tbs_buffer[buf];
171 
172 	/*
173 	 * make sure the allocation is in chunks of 512 bytes
174 	 */
175 	want += align_size;
176 	want &= ~align_size;
177 
178 	current_size = buffer->tb_buffer_size - buffer->tb_buffer_spot;
179 	if (buffer->tb_full && current_size <= 0) {
180 		/*
181 		 * no more data, release this
182 		 * one and go get another
183 		 */
184 
185 		/*
186 		 * tell the reader that a buffer is available
187 		 */
188 		buffer->tb_full = FALSE;
189 		tlm_buffer_release_out_buf(buffers);
190 
191 		buffer = tlm_buffer_advance_out_idx(buffers);
192 		current_size = buffer->tb_buffer_size - buffer->tb_buffer_spot;
193 	}
194 
195 	if (!buffer->tb_full) {
196 		/*
197 		 * next buffer is not full yet.
198 		 * wait for the reader.
199 		 */
200 		tlm_buffer_in_buf_timed_wait(buffers, 500);
201 
202 		buffer = tlm_buffer_out_buf(buffers, NULL);
203 		if (!buffer->tb_full) {
204 			/*
205 			 * we do not have anything from the tape yet
206 			 */
207 			return (0);
208 		}
209 
210 		current_size = buffer->tb_buffer_size - buffer->tb_buffer_spot;
211 	}
212 
213 	/* Make sure we got something */
214 	if (current_size <= 0)
215 		return (NULL);
216 
217 	current_size = min(want, current_size);
218 	rec = &buffer->tb_buffer_data[buffer->tb_buffer_spot];
219 	buffer->tb_buffer_spot += current_size;
220 	*actual_size = current_size;
221 
222 	/*
223 	 * the error flag is only sent back one time,
224 	 * since the flag refers to a previous read
225 	 * attempt, not the data in this buffer.
226 	 */
227 	*error = buffer->tb_errno;
228 
229 	return (rec);
230 }
231 
232 
233 /*
234  * unread a previously read buffer back to the tape buffer
235  */
236 void
237 tlm_unget_read_buffer(tlm_buffers_t *buffers, int size)
238 {
239 	tlm_buffer_t *buffer;
240 	int	align_size = RECORDSIZE - 1;
241 	int	buf;
242 	int	current_size;
243 
244 	buf = buffers->tbs_buffer_out;
245 	buffer = &buffers->tbs_buffer[buf];
246 
247 	/*
248 	 * make sure the allocation is in chunks of 512 bytes
249 	 */
250 	size += align_size;
251 	size &= ~align_size;
252 
253 	current_size = min(size, buffer->tb_buffer_spot);
254 	buffer->tb_buffer_spot -= current_size;
255 }
256 
257 
258 /*
259  * unwrite a previously written buffer
260  */
261 void
262 tlm_unget_write_buffer(tlm_buffers_t *buffers, int size)
263 {
264 	tlm_buffer_t *buffer;
265 	int	align_size = RECORDSIZE - 1;
266 	int	buf;
267 	int	current_size;
268 
269 	buf = buffers->tbs_buffer_in;
270 	buffer = &buffers->tbs_buffer[buf];
271 
272 	/*
273 	 * make sure the allocation is in chunks of 512 bytes
274 	 */
275 	size += align_size;
276 	size &= ~align_size;
277 
278 	current_size = min(size, buffer->tb_buffer_spot);
279 	buffer->tb_buffer_spot -= current_size;
280 }
281 
282 
283 /*
284  * build a checksum for a TAR header record
285  */
286 void
287 tlm_build_header_checksum(tlm_tar_hdr_t *r)
288 {
289 	int	i;
290 	int	sum = 0;
291 	char *c = (char *)r;
292 
293 	(void) memcpy(r->th_chksum, CHKBLANKS, strlen(CHKBLANKS));
294 	for (i = 0; i < RECORDSIZE; i++) {
295 		sum += c[i] & 0xFF;
296 	}
297 	(void) snprintf(r->th_chksum, sizeof (r->th_chksum), "%6o", sum);
298 }
299 
300 /*
301  * verify the tar header checksum
302  */
303 int
304 tlm_vfy_tar_checksum(tlm_tar_hdr_t *tar_hdr)
305 {
306 	int	chksum = oct_atoi(tar_hdr->th_chksum);
307 	uchar_t	*p = (uchar_t *)tar_hdr;
308 	int	sum = 0;	/* initial value of checksum */
309 	int	i;		/* loop counter */
310 
311 	/*
312 	 * compute the checksum
313 	 */
314 	for (i = 0; i < RECORDSIZE; i++) {
315 		sum += p[i] & 0xFF;
316 	}
317 
318 	if (sum == 0) {
319 		NDMP_LOG(LOG_DEBUG,
320 		    "should be %d, is 0", chksum);
321 		/* a zero record ==> end of tar file */
322 		return (0);
323 	}
324 
325 	/*
326 	 * subtract out the label's checksum values
327 	 * this lets us undo the old checksum "in-
328 	 * place", no need to swap blanks in and out
329 	 */
330 	for (i = 0; i < 8; i++) {
331 		sum -= 0xFF & tar_hdr->th_chksum[i];
332 	}
333 
334 	/*
335 	 * replace the old checksum field with blanks
336 	 */
337 	sum += ' ' * 8;
338 
339 	if (sum != chksum)
340 		NDMP_LOG(LOG_DEBUG,
341 		    "should be %d, is %d", chksum, sum);
342 
343 	return ((sum == chksum) ? 1 : -1);
344 }
345 
346 /*
347  * get internal scsi_sasd entry for this tape drive
348  */
349 int
350 tlm_get_scsi_sasd_entry(int lib, int drv)
351 {
352 	int entry;
353 	int i, n;
354 	scsi_link_t *sl;
355 	tlm_drive_t *dp;
356 
357 	entry = -1;
358 	dp = tlm_drive(lib, drv);
359 	if (!dp) {
360 		NDMP_LOG(LOG_DEBUG, "NULL dp for (%d.%d)", lib, drv);
361 	} else if (!dp->td_slink) {
362 		NDMP_LOG(LOG_DEBUG, "NULL dp->td_slink for (%d.%d)", lib, drv);
363 	} else if (!dp->td_slink->sl_sa) {
364 		NDMP_LOG(LOG_DEBUG, "NULL dp->td_slink->sl_sa for (%d.%d)",
365 		    lib, drv);
366 	} else {
367 		/* search through the SASD table */
368 		n = sasd_dev_count();
369 		for (i = 0; i < n; i++) {
370 			sl = sasd_dev_slink(i);
371 			if (!sl)
372 				continue;
373 
374 			if (dp->td_slink->sl_sa == sl->sl_sa &&
375 			    dp->td_scsi_id == sl->sl_sid &&
376 			    dp->td_lun == sl->sl_lun) {
377 				/* all 3 variables match */
378 				entry = i;
379 				break;
380 			}
381 		}
382 	}
383 
384 	return (entry);
385 }
386 
387 /*
388  * get the OS device name for this tape
389  */
390 char *
391 tlm_get_tape_name(int lib, int drv)
392 {
393 	int entry;
394 
395 	entry = tlm_get_scsi_sasd_entry(lib, drv);
396 	if (entry >= 0) {
397 		sasd_drive_t *sd;
398 
399 		if ((sd = sasd_drive(entry)) != 0)
400 			return (sd->sd_name);
401 	}
402 
403 	return ("");
404 }
405 
406 /*
407  * create the IPC area between the reader and writer
408  */
409 tlm_cmd_t *
410 tlm_create_reader_writer_ipc(boolean_t write, long data_transfer_size)
411 {
412 	tlm_cmd_t *cmd;
413 
414 	cmd = ndmp_malloc(sizeof (tlm_cmd_t));
415 	if (cmd == NULL)
416 		return (NULL);
417 
418 	cmd->tc_reader = TLM_BACKUP_RUN;
419 	cmd->tc_writer = TLM_BACKUP_RUN;
420 	cmd->tc_ref = 1;
421 
422 	cmd->tc_buffers = tlm_allocate_buffers(write, data_transfer_size);
423 	if (cmd->tc_buffers == NULL) {
424 		free(cmd);
425 		return (NULL);
426 	}
427 
428 	(void) mutex_init(&cmd->tc_mtx, 0, NULL);
429 	(void) cond_init(&cmd->tc_cv, 0, NULL);
430 
431 	return (cmd);
432 }
433 
434 /*
435  * release(destroy) the IPC between the reader and writer
436  */
437 void
438 tlm_release_reader_writer_ipc(tlm_cmd_t *cmd)
439 {
440 	if (--cmd->tc_ref <= 0) {
441 		(void) mutex_lock(&cmd->tc_mtx);
442 		tlm_release_buffers(cmd->tc_buffers);
443 		(void) cond_destroy(&cmd->tc_cv);
444 		(void) mutex_unlock(&cmd->tc_mtx);
445 		(void) mutex_destroy(&cmd->tc_mtx);
446 		free(cmd);
447 	}
448 }
449 
450 
451 /*
452  * NDMP support begins here.
453  */
454 
455 /*
456  * Initialize the file history callback functions
457  */
458 lbr_fhlog_call_backs_t *
459 lbrlog_callbacks_init(void *cookie, path_hist_func_t log_pname_func,
460     dir_hist_func_t log_dir_func, node_hist_func_t log_node_func)
461 {
462 	lbr_fhlog_call_backs_t *p;
463 
464 	p = ndmp_malloc(sizeof (lbr_fhlog_call_backs_t));
465 	if (p == NULL)
466 		return (NULL);
467 
468 	p->fh_cookie = cookie;
469 	p->fh_logpname = (func_t)log_pname_func;
470 	p->fh_log_dir = (func_t)log_dir_func;
471 	p->fh_log_node = (func_t)log_node_func;
472 	return (p);
473 }
474 
475 /*
476  * Cleanup the callbacks
477  */
478 void
479 lbrlog_callbacks_done(lbr_fhlog_call_backs_t *p)
480 {
481 	if (p != NULL)
482 		(void) free((char *)p);
483 }
484 
485 /*
486  * Call back for file history directory info
487  */
488 int
489 tlm_log_fhdir(tlm_job_stats_t *job_stats, char *dir, struct stat64 *stp,
490     fs_fhandle_t *fhp)
491 {
492 	int rv;
493 	lbr_fhlog_call_backs_t *cbp; /* callbacks pointer */
494 
495 	rv = 0;
496 	if (job_stats == NULL) {
497 		NDMP_LOG(LOG_DEBUG, "log_fhdir: jstat is NULL");
498 	} else if (dir == NULL) {
499 		NDMP_LOG(LOG_DEBUG, "log_fhdir: dir is NULL");
500 	} else if (stp == NULL) {
501 		NDMP_LOG(LOG_DEBUG, "log_fhdir: stp is NULL");
502 	} else if ((cbp = (lbr_fhlog_call_backs_t *)job_stats->js_callbacks)
503 	    == NULL) {
504 		NDMP_LOG(LOG_DEBUG, "log_fhdir: cbp is NULL");
505 	} else if (cbp->fh_log_dir == NULL) {
506 		NDMP_LOG(LOG_DEBUG, "log_fhdir: callback is NULL");
507 	} else
508 		rv = (*cbp->fh_log_dir)(cbp, dir, stp, fhp);
509 
510 	return (rv);
511 }
512 
513 /*
514  * Call back for file history node info
515  */
516 int
517 tlm_log_fhnode(tlm_job_stats_t *job_stats, char *dir, char *file,
518     struct stat64 *stp, u_longlong_t off)
519 {
520 	int rv;
521 	lbr_fhlog_call_backs_t *cbp; /* callbacks pointer */
522 
523 	rv = 0;
524 	if (job_stats == NULL) {
525 		NDMP_LOG(LOG_DEBUG, "log_fhnode: jstat is NULL");
526 	} else if (dir == NULL) {
527 		NDMP_LOG(LOG_DEBUG, "log_fhnode: dir is NULL");
528 	} else if (file == NULL) {
529 		NDMP_LOG(LOG_DEBUG, "log_fhnode: file is NULL");
530 	} else if (stp == NULL) {
531 		NDMP_LOG(LOG_DEBUG, "log_fhnode: stp is NULL");
532 	} else if ((cbp = (lbr_fhlog_call_backs_t *)job_stats->js_callbacks)
533 	    == NULL) {
534 		NDMP_LOG(LOG_DEBUG, "log_fhnode: cbp is NULL");
535 	} else if (cbp->fh_log_node == NULL) {
536 		NDMP_LOG(LOG_DEBUG, "log_fhnode: callback is NULL");
537 	} else
538 		rv = (*cbp->fh_log_node)(cbp, dir, file, stp, off);
539 
540 	return (rv);
541 }
542 
543 /*
544  * Call back for file history path info
545  */
546 int
547 tlm_log_fhpath_name(tlm_job_stats_t *job_stats, char *pathname,
548     struct stat64 *stp, u_longlong_t off)
549 {
550 	int rv;
551 	lbr_fhlog_call_backs_t *cbp; /* callbacks pointer */
552 
553 	rv = 0;
554 	if (!job_stats) {
555 		NDMP_LOG(LOG_DEBUG, "log_fhpath_name: jstat is NULL");
556 	} else if (!pathname) {
557 		NDMP_LOG(LOG_DEBUG, "log_fhpath_name: pathname is NULL");
558 	} else if (!stp) {
559 		NDMP_LOG(LOG_DEBUG, "log_fhpath_name: stp is NULL");
560 	} else if ((cbp = (lbr_fhlog_call_backs_t *)job_stats->js_callbacks)
561 	    == 0) {
562 		NDMP_LOG(LOG_DEBUG, "log_fhpath_name: cbp is NULL");
563 	} else if (!cbp->fh_logpname) {
564 		NDMP_LOG(LOG_DEBUG, "log_fhpath_name: callback is NULL");
565 	} else
566 		rv = (*cbp->fh_logpname)(cbp, pathname, stp, off);
567 
568 	return (rv);
569 }
570 
571 
572 /*
573  * Log call back to report the entry recovery
574  */
575 int
576 tlm_entry_restored(tlm_job_stats_t *job_stats, char *name, int pos)
577 {
578 	lbr_fhlog_call_backs_t *cbp; /* callbacks pointer */
579 
580 	NDMP_LOG(LOG_DEBUG, "name: \"%s\", pos: %d", name, pos);
581 
582 	if (job_stats == NULL) {
583 		NDMP_LOG(LOG_DEBUG, "entry_restored: jstat is NULL");
584 		return (0);
585 	}
586 	cbp = (lbr_fhlog_call_backs_t *)job_stats->js_callbacks;
587 	if (cbp == NULL) {
588 		NDMP_LOG(LOG_DEBUG, "entry_restored is NULL");
589 		return (0);
590 	}
591 	return (*cbp->fh_logpname)(cbp, name, 0, (longlong_t)pos);
592 }
593 /*
594  * NDMP support ends here.
595  */
596 
597 /*
598  * Function: tlm_cat_path
599  * Concatenates two path names
600  * or directory name and file name
601  * into a buffer passed by the caller. A slash
602  * is inserted if required. Buffer is assumed
603  * to hold PATH_MAX characters.
604  *
605  * Parameters:
606  *	char *buf	- buffer to write new dir/name string
607  *	char *dir	- directory name
608  *	char *name	- file name
609  *
610  * Returns:
611  *	TRUE		- No errors. buf contains the dir/name string
612  *	FALSE		- Error. buf is not modified.
613  */
614 boolean_t
615 tlm_cat_path(char *buf, char *dir, char *name)
616 {
617 	char *fmt;
618 	int dirlen = strlen(dir);
619 	int filelen = strlen(name);
620 
621 	if ((dirlen + filelen + 1) >= PATH_MAX) {
622 		return (FALSE);
623 	}
624 
625 	if (*dir == '\0' || *name == '\0' || dir[dirlen - 1] == '/' ||
626 	    *name == '/') {
627 		fmt = "%s%s";
628 	} else {
629 		fmt = "%s/%s";
630 	}
631 
632 	/* check for ".../" and "/...." */
633 	if ((dirlen > 0) && (dir[dirlen - 1] == '/') && (*name == '/'))
634 		name += strspn(name, "/");
635 
636 	/* LINTED variable format */
637 	(void) snprintf(buf, TLM_MAX_PATH_NAME, fmt, dir, name);
638 
639 	return (TRUE);
640 }
641 
642 /*
643  * Get the checkpoint (snapshot) creation time.
644  * This is necessary to check for checkpoints not being stale.
645  */
646 int
647 tlm_get_chkpnt_time(char *path, int auto_checkpoint, time_t *tp, char *jname)
648 {
649 	char volname[TLM_VOLNAME_MAX_LENGTH];
650 	char chk_name[PATH_MAX];
651 	char *cp_nm;
652 
653 	NDMP_LOG(LOG_DEBUG, "path [%s] auto_checkpoint: %d",
654 	    path, auto_checkpoint);
655 
656 	if (path == NULL || *path == '\0' || tp == NULL)
657 		return (-1);
658 
659 	if (get_zfsvolname(volname, TLM_VOLNAME_MAX_LENGTH,
660 	    path) == -1)
661 		return (-1);
662 
663 	if (auto_checkpoint) {
664 		NDMP_LOG(LOG_DEBUG, "volname [%s]", volname);
665 		(void) snprintf(chk_name, PATH_MAX, "%s", jname);
666 		return (chkpnt_creationtime_bypattern(volname, chk_name, tp));
667 	}
668 	cp_nm = strchr(volname, '@');
669 	NDMP_LOG(LOG_DEBUG, "volname [%s] cp_nm [%s]", volname, cp_nm);
670 
671 	return (chkpnt_creationtime_bypattern(volname, cp_nm, tp));
672 }
673 
674 /*
675  * Release an array of pointers and the pointers themselves.
676  */
677 void
678 tlm_release_list(char **lpp)
679 {
680 	char **save;
681 
682 	if ((save = lpp) == 0)
683 		return;
684 
685 	while (*lpp)
686 		free(*lpp++);
687 
688 	free(save);
689 }
690 
691 /*
692  * Print the list of array of strings in the backup log
693  */
694 void
695 tlm_log_list(char *title, char **lpp)
696 {
697 	int i;
698 
699 	if (!lpp)
700 		return;
701 
702 	NDMP_LOG(LOG_DEBUG, "%s:", title);
703 
704 	for (i = 0; *lpp; lpp++, i++)
705 		NDMP_LOG(LOG_DEBUG, "%d: [%s]", i, *lpp);
706 }
707 
708 /*
709  * Insert the backup snapshot name into the path.
710  *
711  * Input:
712  * 	name: Original path name.
713  *
714  * Output:
715  * 	name: Original name modified to include a snapshot.
716  *
717  * Returns:
718  * 	Original name modified to include a snapshot.
719  */
720 char *
721 tlm_build_snapshot_name(char *name, char *sname, char *jname)
722 {
723 	zfs_handle_t *zhp;
724 	char *rest;
725 	char volname[ZFS_MAXNAMELEN];
726 	char mountpoint[PATH_MAX];
727 
728 	if (get_zfsvolname(volname, ZFS_MAXNAMELEN, name) == -1)
729 		goto notzfs;
730 
731 	(void) mutex_lock(&zlib_mtx);
732 	if ((zlibh == NULL) ||
733 	    (zhp = zfs_open(zlibh, volname, ZFS_TYPE_DATASET)) == NULL) {
734 		(void) mutex_unlock(&zlib_mtx);
735 		goto notzfs;
736 	}
737 
738 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, PATH_MAX, NULL,
739 	    NULL, 0, B_FALSE) != 0) {
740 		zfs_close(zhp);
741 		(void) mutex_unlock(&zlib_mtx);
742 		goto notzfs;
743 	}
744 
745 	zfs_close(zhp);
746 	(void) mutex_unlock(&zlib_mtx);
747 
748 	rest = name + strlen(mountpoint);
749 	(void) snprintf(sname, TLM_MAX_PATH_NAME, "%s/%s/%s%s", mountpoint,
750 	    TLM_SNAPSHOT_DIR, jname, rest);
751 
752 	return (sname);
753 
754 notzfs:
755 	(void) strlcpy(sname, name, TLM_MAX_PATH_NAME);
756 	return (sname);
757 }
758 
759 /*
760  * Remove the checkpoint from a path name.
761  *
762  * Input:
763  * 	name: Full pathname with checkpoint embeded.
764  *
765  * Output:
766  * 	unchkp_name: real pathname with no checkpoint.
767  *
768  * Returns:
769  *	Pointer to the un-checkpointed path.
770  */
771 char *
772 tlm_remove_checkpoint(char *name, char *unchkp_name)
773 {
774 	char *cp;
775 	int i;
776 	int plen;
777 
778 	unchkp_name[0] = name[0];
779 	plen = strlen(TLM_SNAPSHOT_PREFIX);
780 	for (i = 1; i <= TLM_VOLNAME_MAX_LENGTH + 1; i++) {
781 		switch (name[i]) {
782 		case '.':
783 			if (strncmp(&name[i], TLM_SNAPSHOT_PREFIX,
784 			    plen) == 0) {
785 				unchkp_name[i] = '\0';
786 				i += plen;
787 				if (name[i] == '\0') {
788 					/*
789 					 * name == "/v1.chkpnt"
790 					 */
791 					return (unchkp_name);
792 				}
793 				if ((cp = strchr(&name[++i], '/')) != NULL) {
794 					(void) strlcat(unchkp_name, cp,
795 					    TLM_VOLNAME_MAX_LENGTH + 1);
796 				}
797 				return (unchkp_name);
798 			} else {
799 				unchkp_name[i] = name[i];
800 			}
801 			break;
802 		case '/':
803 			return (name);
804 		case 0:
805 			return (name);
806 		default:
807 			unchkp_name[i] = name[i];
808 			break;
809 		}
810 	}
811 	return (name);
812 }
813 
814 /*
815  * see if we should exclude this file.
816  */
817 boolean_t
818 tlm_is_excluded(char *dir, char *name, char **excl_files)
819 {
820 	int	i;
821 	char	full_name[TLM_MAX_PATH_NAME];
822 
823 	if (!dir || !name || !excl_files)
824 		return (FALSE);
825 
826 	if (!tlm_cat_path(full_name, dir, name)) {
827 		NDMP_LOG(LOG_DEBUG, "Path too long [%s][%s]",
828 		    dir, name);
829 		return (FALSE);
830 	}
831 	for (i = 0; excl_files[i] != 0; i++) {
832 		if (match(excl_files[i], full_name)) {
833 			return (TRUE);
834 		}
835 	}
836 	return (FALSE);
837 }
838 
839 /*
840  * Check if the path is too long
841  */
842 boolean_t
843 tlm_is_too_long(int checkpointed, char *dir, char *nm)
844 {
845 	int nlen, tot;
846 
847 	tot = 0;
848 	if (dir)
849 		tot += strlen(dir);
850 	if (checkpointed)
851 		tot += strlen(TLM_SNAPSHOT_DIR) + 1;
852 	if (nm) {
853 		if ((nlen = strlen(nm)) > 0)
854 			tot += nlen + 1;
855 	}
856 	return ((tot >= PATH_MAX) ? TRUE : FALSE);
857 }
858 
859 /*
860  * Get the data offset of inside the buffer
861  */
862 longlong_t
863 tlm_get_data_offset(tlm_cmd_t *lcmds)
864 {
865 	if (!lcmds)
866 		return (0LL);
867 
868 	return (lcmds->tc_buffers->tbs_offset);
869 }
870 
871 /*
872  * Enable the barcode capability on the library
873  */
874 void
875 tlm_enable_barcode(int l)
876 {
877 	tlm_library_t *lp;
878 
879 	if ((lp = tlm_library(l))) {
880 		lp->tl_capability_barcodes = TRUE;
881 		NDMP_LOG(LOG_DEBUG,
882 		    "Barcode capability on library %d enabled.", l);
883 	}
884 }
885 
886 /*
887  * SASD SCSI support
888  */
889 static scsi_adapter_t my_sa;
890 static int sasd_drive_count = 0;
891 static scsi_sasd_drive_t *scsi_sasd_drives[128];
892 
893 /*
894  * Count of SCSI devices
895  */
896 int
897 sasd_dev_count(void)
898 {
899 	return (sasd_drive_count);
900 }
901 
902 /*
903  * Return the SCSI device name
904  */
905 char *
906 sasd_slink_name(scsi_link_t *slink)
907 {
908 	int i;
909 
910 	for (i = 0; i < sasd_drive_count; i++) {
911 		if (&scsi_sasd_drives[i]->ss_slink == slink)
912 			return (scsi_sasd_drives[i]->ss_sd.sd_name);
913 	}
914 	return (NULL);
915 }
916 
917 /*
918  * Return the SCSI drive structure
919  */
920 sasd_drive_t *
921 sasd_slink_drive(scsi_link_t *slink)
922 {
923 	int i;
924 
925 	for (i = 0; i < sasd_drive_count; i++) {
926 		if (&scsi_sasd_drives[i]->ss_slink == slink)
927 			return (&scsi_sasd_drives[i]->ss_sd);
928 	}
929 	return (NULL);
930 }
931 
932 /*
933  * Return the SCSI link pointer for the given index
934  */
935 scsi_link_t *
936 sasd_dev_slink(int entry)
937 {
938 	scsi_link_t *rv;
939 
940 	if (entry >= 0 && entry < sasd_drive_count)
941 		rv = &scsi_sasd_drives[entry]->ss_slink;
942 	else
943 		rv = NULL;
944 
945 	return (rv);
946 }
947 
948 /*
949  * Return the SCSI drive for the given index
950  */
951 sasd_drive_t *
952 sasd_drive(int entry)
953 {
954 	sasd_drive_t *rv;
955 
956 	if (entry >= 0 && entry < sasd_drive_count)
957 		rv = &scsi_sasd_drives[entry]->ss_sd;
958 	else
959 		rv = NULL;
960 
961 	return (rv);
962 }
963 
964 /*
965  * Attach the SCSI device by updating the structures
966  */
967 void
968 scsi_sasd_attach(scsi_adapter_t *sa, int sid, int lun, char *name,
969     int type)
970 {
971 	scsi_link_t *sl, *next;
972 	scsi_sasd_drive_t *ssd;
973 
974 	ssd = ndmp_malloc(sizeof (scsi_sasd_drive_t));
975 	if (ssd == NULL)
976 		return;
977 
978 	scsi_sasd_drives[sasd_drive_count++] = ssd;
979 
980 	switch (type) {
981 	case DTYPE_CHANGER:
982 		(void) snprintf(ssd->ss_sd.sd_name,
983 		    sizeof (ssd->ss_sd.sd_name), "%s/%s", SCSI_CHANGER_DIR,
984 		    name);
985 		break;
986 	case DTYPE_SEQUENTIAL:
987 		(void) snprintf(ssd->ss_sd.sd_name,
988 		    sizeof (ssd->ss_sd.sd_name), "%s/%s", SCSI_TAPE_DIR, name);
989 		break;
990 	}
991 
992 	sl = &ssd->ss_slink;
993 	sl->sl_type = type;
994 	sl->sl_sa = sa;
995 	sl->sl_lun = lun;
996 	sl->sl_sid = sid;
997 	sl->sl_requested_max_active = 1;
998 
999 	/* Insert slink */
1000 	next = sa->sa_link_head.sl_next;
1001 	sa->sa_link_head.sl_next = sl;
1002 	sl->sl_next = next;
1003 }
1004 
1005 /*
1006  * Go through the attached devices and detect the tape
1007  * and robot by checking the /dev entries
1008  */
1009 int
1010 probe_scsi(void)
1011 {
1012 	DIR *dirp;
1013 	struct dirent *dp;
1014 	scsi_adapter_t *sa = &my_sa;
1015 	char *p;
1016 	int lun = 0;
1017 	int sid = 0;
1018 
1019 	/* Initialize the scsi adapter link */
1020 	sa->sa_link_head.sl_next = &sa->sa_link_head;
1021 
1022 	/* Scan for the changer */
1023 	dirp = opendir(SCSI_CHANGER_DIR);
1024 	if (dirp == NULL) {
1025 		NDMP_LOG(LOG_DEBUG,
1026 		    "Changer directory read error %s", SCSI_CHANGER_DIR);
1027 	} else {
1028 		while ((dp = readdir(dirp)) != NULL) {
1029 			if ((strcmp(dp->d_name, ".") == 0) ||
1030 			    (strcmp(dp->d_name, "..") == 0))
1031 				continue;
1032 
1033 			if ((p = strchr(dp->d_name, 'd')) != NULL) {
1034 				lun = atoi(++p);
1035 				p = strchr(dp->d_name, 't');
1036 				sid = atoi(++p);
1037 			}
1038 			else
1039 				sid = atoi(dp->d_name);
1040 
1041 			scsi_sasd_attach(sa, 0, lun, dp->d_name,
1042 			    DTYPE_CHANGER);
1043 		}
1044 		(void) closedir(dirp);
1045 	}
1046 
1047 	/* Scan for tape drives */
1048 	dirp = opendir(SCSI_TAPE_DIR);
1049 	if (dirp == NULL) {
1050 		NDMP_LOG(LOG_DEBUG,
1051 		    "Tape directory read error %s", SCSI_TAPE_DIR);
1052 	} else {
1053 		while ((dp = readdir(dirp)) != NULL) {
1054 			if ((strcmp(dp->d_name, ".") == 0) ||
1055 			    (strcmp(dp->d_name, "..") == 0))
1056 				continue;
1057 
1058 			/* Skip special modes */
1059 			if (strpbrk(dp->d_name, "bchlmu") != 0)
1060 				continue;
1061 
1062 			/* Pick the non-rewind device */
1063 			if (strchr(dp->d_name, 'n') == NULL)
1064 				continue;
1065 
1066 			sid = atoi(dp->d_name);
1067 
1068 			/*
1069 			 * SCSI ID should match with the ID of the device
1070 			 * (will be checked by SCSI get elements page later)
1071 			 */
1072 			scsi_sasd_attach(sa, sid, 0, dp->d_name,
1073 			    DTYPE_SEQUENTIAL);
1074 		}
1075 		(void) closedir(dirp);
1076 	}
1077 
1078 	return (0);
1079 }
1080 
1081 /*
1082  * Get the SCSI device type (tape, robot)
1083  */
1084 /*ARGSUSED*/
1085 int
1086 scsi_get_devtype(char *adapter, int sid, int lun)
1087 {
1088 	int rv;
1089 	scsi_adapter_t *sa = &my_sa;
1090 	scsi_link_t *sl, *sh;
1091 
1092 	rv = -1;
1093 	sh = &sa->sa_link_head;
1094 	for (sl = sh->sl_next; sl != sh; sl = sl->sl_next)
1095 		if (sl->sl_sid == sid && sl->sl_lun == lun)
1096 			rv = sl->sl_type;
1097 
1098 	return (rv);
1099 }
1100 
1101 
1102 /*
1103  * Check if the SCSI device exists
1104  */
1105 /*ARGSUSED*/
1106 int
1107 scsi_dev_exists(char *adapter, int sid, int lun)
1108 {
1109 	scsi_adapter_t *sa = &my_sa;
1110 	scsi_link_t *sl, *sh;
1111 
1112 	sh = &sa->sa_link_head;
1113 	for (sl = sh->sl_next; sl != sh; sl = sl->sl_next)
1114 		if (sl->sl_sid == sid && sl->sl_lun == lun)
1115 			return (1);
1116 	return (0);
1117 }
1118 
1119 
1120 /*
1121  * Count of SCSI adapters
1122  */
1123 int
1124 scsi_get_adapter_count(void)
1125 {
1126 	/* Currently support one adapter only */
1127 	return (1);
1128 }
1129 
1130 /*
1131  * Return the SCSI adapter structure
1132  */
1133 /*ARGSUSED*/
1134 scsi_adapter_t *
1135 scsi_get_adapter(int adapter)
1136 {
1137 	return (&my_sa);
1138 }
1139 
1140 /*
1141  * IOCTL wrapper with retries
1142  */
1143 int
1144 tlm_ioctl(int fd, int cmd, void *data)
1145 {
1146 	int retries = 0;
1147 
1148 	NDMP_LOG(LOG_DEBUG, "tlm_ioctl fd %d cmd %d", fd, cmd);
1149 	if (fd == 0 || data == NULL)
1150 		return (EINVAL);
1151 
1152 	do {
1153 		if (ioctl(fd, cmd, data) == 0)
1154 			break;
1155 
1156 		if (errno != EIO && errno != 0) {
1157 			NDMP_LOG(LOG_ERR,
1158 			    "Failed to send command to device: %m.");
1159 			NDMP_LOG(LOG_DEBUG, "IOCTL error %d", errno);
1160 			return (errno);
1161 		}
1162 		(void) sleep(1);
1163 	} while (retries++ < MAXIORETRY);
1164 
1165 	return (0);
1166 }
1167 
1168 /*
1169  * Checkpoint or snapshot calls
1170  */
1171 
1172 /*
1173  * Create a snapshot on the volume
1174  */
1175 int
1176 chkpnt_backup_prepare(char *volname, char *jname, boolean_t recursive)
1177 {
1178 	char chk_name[PATH_MAX];
1179 	char *p;
1180 	int rv;
1181 
1182 	if (!volname || !*volname)
1183 		return (-1);
1184 
1185 	/* Should also return -1 if checkpoint not enabled */
1186 
1187 	/* Remove the leading slash */
1188 	p = volname;
1189 	while (*p == '/')
1190 		p++;
1191 
1192 	(void) snprintf(chk_name, PATH_MAX, "%s@%s", p, jname);
1193 
1194 	(void) mutex_lock(&zlib_mtx);
1195 	if ((rv = zfs_snapshot(zlibh, chk_name, recursive, NULL))
1196 	    == -1) {
1197 		if (errno == EEXIST) {
1198 			(void) mutex_unlock(&zlib_mtx);
1199 			return (0);
1200 		}
1201 		NDMP_LOG(LOG_DEBUG,
1202 		    "chkpnt_backup_prepare: %s failed (err=%d): %s",
1203 		    chk_name, errno, libzfs_error_description(zlibh));
1204 		(void) mutex_unlock(&zlib_mtx);
1205 		return (rv);
1206 	}
1207 	(void) mutex_unlock(&zlib_mtx);
1208 	return (0);
1209 }
1210 
1211 /*
1212  * Remove the 'backup' snapshot if backup was successful
1213  */
1214 int
1215 chkpnt_backup_successful(char *volname, char *jname, boolean_t recursive,
1216     int *zfs_err)
1217 {
1218 	char chk_name[PATH_MAX];
1219 	zfs_handle_t *zhp;
1220 	zfs_type_t ztype;
1221 	int err;
1222 	char *p;
1223 
1224 	if (zfs_err)
1225 		*zfs_err = 0;
1226 
1227 	if (!volname || !*volname)
1228 		return (-1);
1229 
1230 	/* Should also return -1 if checkpoint not enabled */
1231 
1232 	/* Remove the leading slash */
1233 	p = volname;
1234 	while (*p == '/')
1235 		p++;
1236 
1237 	if (recursive) {
1238 		ztype = ZFS_TYPE_VOLUME | ZFS_TYPE_FILESYSTEM;
1239 	} else {
1240 		(void) snprintf(chk_name, PATH_MAX, "%s@%s", p, jname);
1241 		p = chk_name;
1242 		ztype = ZFS_TYPE_SNAPSHOT;
1243 	}
1244 
1245 	(void) mutex_lock(&zlib_mtx);
1246 	if ((zhp = zfs_open(zlibh, p, ztype)) == NULL) {
1247 		NDMP_LOG(LOG_DEBUG, "chkpnt_backup_successful: open %s failed",
1248 		    p);
1249 		(void) mutex_unlock(&zlib_mtx);
1250 		return (-1);
1251 	}
1252 
1253 	if (recursive) {
1254 		err = zfs_destroy_snaps(zhp, jname, B_TRUE);
1255 	} else {
1256 		err = zfs_destroy(zhp, B_TRUE);
1257 	}
1258 
1259 	if (err) {
1260 		NDMP_LOG(LOG_ERR, "%s (recursive destroy: %d): %d; %s; %s",
1261 		    p,
1262 		    recursive,
1263 		    libzfs_errno(zlibh),
1264 		    libzfs_error_action(zlibh),
1265 		    libzfs_error_description(zlibh));
1266 
1267 		if (zfs_err)
1268 			*zfs_err = err;
1269 	}
1270 
1271 	zfs_close(zhp);
1272 	(void) mutex_unlock(&zlib_mtx);
1273 
1274 	return (0);
1275 }
1276 
1277 /*
1278  * Get the snapshot creation time
1279  */
1280 int
1281 chkpnt_creationtime_bypattern(char *volname, char *pattern, time_t *tp)
1282 {
1283 	char chk_name[PATH_MAX];
1284 	zfs_handle_t *zhp;
1285 	char *p;
1286 
1287 	if (!volname || !*volname)
1288 		return (-1);
1289 
1290 	/* Should also return -1 if checkpoint not enabled */
1291 
1292 	/* Remove the leading slash */
1293 	p = volname;
1294 	while (*p == '/')
1295 		p++;
1296 
1297 	(void) strlcpy(chk_name, p, PATH_MAX);
1298 	(void) strlcat(chk_name, "@", PATH_MAX);
1299 	(void) strlcat(chk_name, pattern, PATH_MAX);
1300 
1301 	(void) mutex_lock(&zlib_mtx);
1302 	if ((zhp = zfs_open(zlibh, chk_name, ZFS_TYPE_DATASET)) == NULL) {
1303 		NDMP_LOG(LOG_DEBUG, "chkpnt_creationtime: open %s failed",
1304 		    chk_name);
1305 		(void) mutex_unlock(&zlib_mtx);
1306 		return (-1);
1307 	}
1308 
1309 	*tp = zfs_prop_get_int(zhp, ZFS_PROP_CREATION);
1310 	zfs_close(zhp);
1311 	(void) mutex_unlock(&zlib_mtx);
1312 
1313 	return (0);
1314 }
1315 
1316 
1317 /*
1318  * Get the ZFS volume name out of the given path
1319  */
1320 int
1321 get_zfsvolname(char *volname, int len, char *path)
1322 {
1323 	struct stat64 stbuf;
1324 	struct extmnttab ent;
1325 	FILE *mntfp;
1326 	int rv;
1327 
1328 	*volname = '\0';
1329 	if (stat64(path, &stbuf) != 0) {
1330 		return (-1);
1331 	}
1332 
1333 	if ((mntfp = fopen(MNTTAB, "r")) == NULL) {
1334 		return (-1);
1335 	}
1336 	while ((rv = getextmntent(mntfp, &ent, 0)) == 0) {
1337 		if (makedevice(ent.mnt_major, ent.mnt_minor) ==
1338 		    stbuf.st_dev)
1339 			break;
1340 	}
1341 
1342 	if (rv == 0 &&
1343 	    strcmp(ent.mnt_fstype, MNTTYPE_ZFS) == 0)
1344 		(void) strlcpy(volname, ent.mnt_special, len);
1345 	else
1346 		rv = -1;
1347 
1348 	(void) fclose(mntfp);
1349 	return (rv);
1350 }
1351 
1352 
1353 /*
1354  * Check if the volume type is snapshot volume
1355  */
1356 boolean_t
1357 fs_is_chkpntvol(char *path)
1358 {
1359 	zfs_handle_t *zhp;
1360 	char vol[ZFS_MAXNAMELEN];
1361 
1362 	if (!path || !*path)
1363 		return (FALSE);
1364 
1365 	if (get_zfsvolname(vol, sizeof (vol), path) == -1)
1366 		return (FALSE);
1367 
1368 	(void) mutex_lock(&zlib_mtx);
1369 	if ((zhp = zfs_open(zlibh, vol, ZFS_TYPE_DATASET)) == NULL) {
1370 		(void) mutex_unlock(&zlib_mtx);
1371 		return (FALSE);
1372 	}
1373 
1374 	if (zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) {
1375 		zfs_close(zhp);
1376 		(void) mutex_unlock(&zlib_mtx);
1377 		return (FALSE);
1378 	}
1379 	zfs_close(zhp);
1380 	(void) mutex_unlock(&zlib_mtx);
1381 
1382 	return (TRUE);
1383 }
1384 
1385 /*
1386  * Check if the volume is capable of checkpoints
1387  */
1388 boolean_t
1389 fs_is_chkpnt_enabled(char *path)
1390 {
1391 	zfs_handle_t *zhp;
1392 	char vol[ZFS_MAXNAMELEN];
1393 
1394 	if (!path || !*path)
1395 		return (FALSE);
1396 
1397 	(void) mutex_lock(&zlib_mtx);
1398 	if (get_zfsvolname(vol, sizeof (vol), path) == -1) {
1399 		(void) mutex_unlock(&zlib_mtx);
1400 		return (FALSE);
1401 	}
1402 
1403 	if ((zhp = zfs_open(zlibh, vol, ZFS_TYPE_DATASET)) == NULL) {
1404 		(void) mutex_unlock(&zlib_mtx);
1405 		return (FALSE);
1406 	}
1407 	zfs_close(zhp);
1408 	(void) mutex_unlock(&zlib_mtx);
1409 
1410 	return (TRUE);
1411 }
1412 
1413 /*
1414  * Check if the volume is read-only
1415  */
1416 boolean_t
1417 fs_is_rdonly(char *path)
1418 {
1419 	return (fs_is_chkpntvol(path));
1420 }
1421 
1422 /*
1423  * Min/max functions
1424  */
1425 unsigned
1426 min(a, b)
1427 	unsigned a, b;
1428 {
1429 	return (a < b ? a : b);
1430 }
1431 
1432 unsigned
1433 max(a, b)
1434 	unsigned a, b;
1435 {
1436 	return (a > b ? a : b);
1437 }
1438 
1439 longlong_t
1440 llmin(longlong_t a, longlong_t b)
1441 {
1442 	return (a < b ? a : b);
1443 }
1444