1 /* 2 * Copyright 2009 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 #include <stdlib.h> 40 #include <ctype.h> 41 #include <stdio.h> 42 #include <limits.h> 43 #include <string.h> 44 #include <time.h> 45 #include <sys/types.h> 46 #include <sys/acl.h> 47 #include <utime.h> 48 #include <unistd.h> 49 #include <pthread.h> 50 #include <archives.h> 51 #include <priv.h> 52 #include <tlm.h> 53 #include <libzfs.h> 54 #include <pwd.h> 55 #include <grp.h> 56 #include <ndmpd_prop.h> 57 #include "tlm_proto.h" 58 59 60 #define PM_EXACT_OR_CHILD(m) ((m) == PM_EXACT || (m) == PM_CHILD) 61 62 typedef boolean_t name_match_fp_t(char *s, char *t); 63 64 static void set_acl(char *name, 65 tlm_acls_t *acls); 66 static long restore_file(int *fp, 67 char *real_name, 68 long size, 69 longlong_t huge_size, 70 tlm_acls_t *, 71 boolean_t want_this_file, 72 tlm_cmd_t *, 73 tlm_job_stats_t *); 74 static long restore_xattr_hdr(int *fp, 75 char *name, 76 char *fname, 77 long size, 78 tlm_acls_t *acls, 79 tlm_cmd_t *local_commands, 80 tlm_job_stats_t *job_stats); 81 static int get_long_name(int lib, 82 int drv, 83 long recsize, 84 char *name, 85 long *buf_spot, 86 tlm_cmd_t *local_commands); 87 static int get_humongus_file_header(int lib, 88 int drv, 89 long recsize, 90 longlong_t *size, 91 char *name, 92 tlm_cmd_t *); 93 static int create_directory(char *dir, 94 tlm_job_stats_t *); 95 static int create_hard_link(char *name, 96 char *link, 97 tlm_acls_t *, 98 tlm_job_stats_t *); 99 static int create_sym_link(char *dst, 100 char *target, 101 tlm_acls_t *, 102 tlm_job_stats_t *); 103 static int create_fifo(char *name, 104 tlm_acls_t *); 105 static long load_acl_info(int lib, 106 int drv, 107 long size, 108 tlm_acls_t *, 109 long *acl_spot, 110 tlm_cmd_t *); 111 static char *get_read_buffer(int want, 112 int *error, 113 int *actual_size, 114 tlm_cmd_t *); 115 static boolean_t wildcard_enabled(void); 116 static boolean_t is_file_wanted(char *name, 117 char **sels, 118 char **exls, 119 int flags, 120 int *mchtype, 121 int *pos); 122 static char *catnames(struct rs_name_maker *rnp, 123 char *buf, 124 int pos, 125 char *path); 126 127 static char *rs_new_name(struct rs_name_maker *rnp, 128 char *real_name, 129 int pos, 130 char *path); 131 132 typedef struct stack_ent { 133 char *se_name; 134 tlm_acls_t se_acls; 135 } stack_ent_t; 136 137 138 /* 139 * dtree_push 140 */ 141 int 142 dtree_push(cstack_t *stp, char *nmp, tlm_acls_t *acls) 143 { 144 int len; 145 stack_ent_t *sp; 146 147 sp = ndmp_malloc(sizeof (stack_ent_t)); 148 if (!sp || !nmp || !acls) { 149 free(sp); 150 return (-1); 151 } 152 153 len = strlen(nmp) + 1; 154 sp->se_name = ndmp_malloc(len); 155 if (!sp->se_name) { 156 free(sp); 157 return (-1); 158 } 159 160 (void) strlcpy(sp->se_name, nmp, len); 161 (void) memcpy(&sp->se_acls, acls, sizeof (*acls)); 162 (void) memset(acls, 0, sizeof (tlm_acls_t)); 163 164 return (cstack_push(stp, (void *)sp, sizeof (*sp))); 165 } 166 167 /* 168 * dtree_pop 169 */ 170 int 171 dtree_pop(cstack_t *stp) 172 { 173 int err; 174 stack_ent_t *sp; 175 176 err = cstack_pop(stp, (void **)&sp, (void *)NULL); 177 if (err) 178 return (-1); 179 180 set_acl(sp->se_name, &sp->se_acls); 181 182 free(sp->se_name); 183 free(sp); 184 return (err); 185 } 186 187 188 /* 189 * dtree_peek 190 */ 191 char * 192 dtree_peek(cstack_t *stp) 193 { 194 int err; 195 stack_ent_t *sp; 196 197 err = cstack_top(stp, (void **)&sp, (void *)NULL); 198 if (err) 199 return (NULL); 200 201 return (sp->se_name); 202 } 203 204 /* 205 * NBU and EBS may not send us the correct file list containing hardlinks 206 * during a DAR restore, e.g. they appear always send the first name 207 * associated with an inode, even if other link names were 208 * selected for the restore. As a workaround, we use the file name entry 209 * in sels[] (ignore the name in the tar header) as restore target. 210 */ 211 static char * 212 rs_darhl_new_name(struct rs_name_maker *rnp, char *name, char **sels, int *pos, 213 char *longname) 214 { 215 int x; 216 217 for (x = 0; sels[x] != NULL; x++) { 218 if (strcmp(sels[x], " ")) { 219 *pos = x; 220 (void) strlcpy(longname, sels[x], TLM_MAX_PATH_NAME); 221 NDMP_LOG(LOG_DEBUG, 222 "to replace hardlink name [%s], pos [%d]", 223 longname, *pos); 224 225 return (rs_new_name(rnp, name, *pos, longname)); 226 } 227 } 228 229 return (NULL); 230 } 231 232 233 /* 234 * Main dir restore funciton for tar 235 */ 236 int 237 tar_getdir(tlm_commands_t *commands, 238 tlm_cmd_t *local_commands, 239 tlm_job_stats_t *job_stats, 240 struct rs_name_maker *rnp, 241 int lib, 242 int drv, 243 char **sels, /* what to get off the tape */ 244 char **exls, /* what to leave behind */ 245 int flags, 246 int DAR, struct hardlink_q *hardlink_q) 247 { 248 int fp = 0; /* file being restored ... */ 249 /* ...need to preserve across volume changes */ 250 tlm_acls_t *acls; /* file access info */ 251 char *longname; 252 boolean_t is_long_name = FALSE; 253 char *longlink; 254 char *hugename; 255 longlong_t huge_size = 0; /* size of a HUGE file */ 256 long acl_spot; /* any ACL info on the next volume */ 257 long file_size; /* size of file to restore */ 258 long size_left = 0; /* need this after volume change */ 259 int last_action = 0; /* what we are doing at EOT */ 260 boolean_t multi_volume = FALSE; /* is this a multi-volume switch ? */ 261 int chk_rv; /* scratch area */ 262 263 int mchtype, pos; 264 /* 265 * if an exact match is found for 266 * restore and its position in the 267 * selections list 268 */ 269 int nzerohdr; /* the number of empty tar headers */ 270 boolean_t break_flg; /* exit the while loop */ 271 int rv; 272 long nm_end, lnk_end; 273 char *name, *nmp; 274 cstack_t *stp; 275 char *bkpath; 276 char *parentlnk; 277 /* 278 * The directory where temporary files may be created during a partial 279 * non-DAR restore of hardlinks. It is intended to be initialized by 280 * an environment variable that can be set by user. 281 * 282 * It is not initialized for now. We keep it here for future use. 283 */ 284 char *tmplink_dir = NULL; 285 int dar_recovered = 0; 286 287 /* 288 * startup 289 */ 290 291 longname = ndmp_malloc(TLM_MAX_PATH_NAME); 292 longlink = ndmp_malloc(TLM_MAX_PATH_NAME); 293 hugename = ndmp_malloc(TLM_MAX_PATH_NAME); 294 parentlnk = ndmp_malloc(TLM_MAX_PATH_NAME); 295 name = ndmp_malloc(TLM_MAX_PATH_NAME); 296 acls = ndmp_malloc(sizeof (tlm_acls_t)); 297 stp = cstack_new(); 298 if (longname == NULL || longlink == NULL || hugename == NULL || 299 name == NULL || acls == NULL || stp == NULL || parentlnk == NULL) { 300 cstack_delete(stp); 301 free(longname); 302 free(longlink); 303 free(hugename); 304 free(parentlnk); 305 free(name); 306 free(acls); 307 return (-TLM_NO_SCRATCH_SPACE); 308 } 309 310 acl_spot = 0; 311 *hugename = '\0'; 312 *parentlnk = '\0'; 313 nm_end = 0; 314 *longname = '\0'; 315 lnk_end = 0; 316 *longlink = '\0'; 317 (void) memset(acls, 0, sizeof (tlm_acls_t)); 318 if (IS_SET(flags, RSFLG_OVR_ALWAYS)) { 319 acls->acl_overwrite = TRUE; 320 NDMP_LOG(LOG_DEBUG, "RSFLG_OVR_ALWAYS"); 321 } else if (IS_SET(flags, RSFLG_OVR_UPDATE)) { 322 acls->acl_update = TRUE; 323 NDMP_LOG(LOG_DEBUG, "RSFLG_OVR_UPDATE"); 324 } 325 326 /* 327 * work 328 */ 329 rv = 0; 330 nzerohdr = 0; 331 break_flg = FALSE; 332 while (commands->tcs_writer != TLM_ABORT && 333 local_commands->tc_writer != TLM_STOP) { 334 tlm_tar_hdr_t fake_tar_hdr; 335 char *file_name; 336 char *link_name; 337 int erc; 338 int actual_size; 339 boolean_t want_this_file; 340 int want = sizeof (tlm_tar_hdr_t); 341 tlm_tar_hdr_t *tar_hdr; 342 343 /* The inode of an LF_LINK type. */ 344 unsigned long hardlink_inode = 0; 345 346 /* 347 * Indicate whether a file with the same inode has been 348 * restored. 349 */ 350 int hardlink_done = 0; 351 352 /* The path of the restored hardlink file */ 353 char *hardlink_target = NULL; 354 int is_hardlink = 0; 355 356 /* 357 * Whether a temporary file should be created for restoring 358 * hardlink. 359 */ 360 int hardlink_tmp_file = 0; 361 char *hardlink_tmp_name = ".tmphlrsnondar"; 362 363 /* used to make up hardlink_tmp_name */ 364 static int hardlink_tmp_idx = 0; 365 366 if (break_flg) { 367 NDMP_LOG(LOG_DEBUG, 368 "Exiting writer thread drive %d", drv); 369 break; 370 } 371 372 if (multi_volume) { 373 NDMP_LOG(LOG_DEBUG, "multi_volume %c %d", 374 last_action, size_left); 375 376 /* 377 * the previous volume is out of data 378 * and is back in the rack, a new tape 379 * is loaded and ready to read. 380 * 381 * We need to pick up where we left off. 382 */ 383 (void) memset(&fake_tar_hdr, 0, sizeof (fake_tar_hdr)); 384 file_size = size_left; 385 tar_hdr = &fake_tar_hdr; 386 tar_hdr->th_linkflag = last_action; 387 388 multi_volume = FALSE; 389 last_action = 0; 390 } else { 391 tar_hdr = (tlm_tar_hdr_t *)get_read_buffer(want, 392 &erc, &actual_size, local_commands); 393 394 if (tar_hdr == NULL) { 395 rv = -1; 396 continue; 397 } 398 399 /* 400 * we can ignore read errors here because 401 * 1) they are logged by Restore Reader 402 * 2) we are not doing anything important here 403 * just looking for the next work record. 404 */ 405 if (actual_size < want) { 406 /* 407 * EOF hits here 408 * 409 * wait for another buffer to come along 410 * or until the Reader thread tells us 411 * that no more tapes will be loaded ... 412 * time to stop. 413 */ 414 continue; 415 } 416 417 /* 418 * check for "we are lost" 419 */ 420 chk_rv = tlm_vfy_tar_checksum(tar_hdr); 421 if (chk_rv == 0) { 422 /* one of the end of tar file marks */ 423 if (++nzerohdr >= 2) { 424 NDMP_LOG(LOG_DEBUG, 425 "nzerohdr %d, breaking", 426 nzerohdr); 427 /* end of tar file */ 428 break; 429 } 430 NDMP_LOG(LOG_DEBUG, "nzerohdr %d, continuing", 431 nzerohdr); 432 continue; 433 } else if (chk_rv < 0) { 434 nzerohdr = 0; 435 /* skip this record */ 436 continue; 437 } 438 nzerohdr = 0; 439 440 /* 441 * When files are spanned to the next tape, the 442 * information of the acls must not be over-written 443 * by the information of the LF_MULTIVOL and LF_VOLHDR 444 * header, whose information is irrelevant to the file. 445 * The information of the original header must be 446 * kept in the 'acl'. 447 */ 448 if (tar_hdr->th_linkflag != LF_MULTIVOL && 449 tar_hdr->th_linkflag != LF_VOLHDR) { 450 if (tar_hdr->th_linkflag != LF_HUMONGUS) { 451 acls->acl_attr.st_mode = 452 oct_atoi(tar_hdr->th_mode); 453 acls->acl_attr.st_size = 454 oct_atoi(tar_hdr->th_size); 455 acls->acl_attr.st_uid = 456 oct_atoi(tar_hdr->th_uid); 457 acls->acl_attr.st_gid = 458 oct_atoi(tar_hdr->th_gid); 459 acls->acl_attr.st_mtime = 460 oct_atoi(tar_hdr->th_mtime); 461 (void) strlcpy(acls->uname, 462 tar_hdr->th_uname, 463 sizeof (acls->uname)); 464 (void) strlcpy(acls->gname, 465 tar_hdr->th_gname, 466 sizeof (acls->gname)); 467 } 468 file_size = oct_atoi(tar_hdr->th_size); 469 acl_spot = 0; 470 last_action = tar_hdr->th_linkflag; 471 } 472 } 473 474 NDMP_LOG(LOG_DEBUG, "n [%s] f [%c] s %lld m %o u %d g %d t %d", 475 tar_hdr->th_name, tar_hdr->th_linkflag, 476 acls->acl_attr.st_size, acls->acl_attr.st_mode, 477 acls->acl_attr.st_uid, acls->acl_attr.st_gid, 478 acls->acl_attr.st_mtime); 479 480 /* 481 * If the restore is running using DAR we should check for 482 * extended attribute entries 483 */ 484 if (dar_recovered && 485 tar_hdr->th_linkflag != LF_XATTR) 486 break; 487 488 switch (tar_hdr->th_linkflag) { 489 case LF_MULTIVOL: 490 multi_volume = TRUE; 491 break; 492 case LF_LINK: 493 is_hardlink = 1; 494 hardlink_inode = 495 oct_atoi(tar_hdr->th_shared.th_hlink_ino); 496 497 /* 498 * Check if we have restored a link with the same inode 499 * If the inode is 0, we have to restore it as a 500 * regular file. 501 */ 502 if (hardlink_inode) { 503 hardlink_done = !hardlink_q_get(hardlink_q, 504 hardlink_inode, 0, &hardlink_target); 505 } 506 507 if (hardlink_done) { 508 NDMP_LOG(LOG_DEBUG, 509 "found hardlink, inode = %u, target = [%s]", 510 hardlink_inode, 511 hardlink_target? hardlink_target : "--"); 512 513 /* create a hardlink to hardlink_target */ 514 file_name = (*longname == 0) ? 515 tar_hdr->th_name : longname; 516 if (!is_file_wanted(file_name, sels, exls, 517 flags, &mchtype, &pos)) { 518 nmp = NULL; 519 /* 520 * This means that DMA did not send us 521 * the correct fh_info for the file 522 * in restore list. We use the file 523 * name entry in sels[] (ignore the 524 * name in the tar header) as restore 525 * target. 526 */ 527 if (DAR) { 528 nmp = rs_darhl_new_name(rnp, 529 name, sels, &pos, 530 file_name); 531 } 532 } else { 533 nmp = rs_new_name(rnp, name, pos, 534 file_name); 535 if (!nmp) { 536 NDMP_LOG(LOG_DEBUG, 537 "can't make name for %s", 538 longname); 539 } 540 } 541 542 if (nmp) { 543 if (hardlink_target) { 544 erc = create_hard_link( 545 hardlink_target, nmp, 546 acls, job_stats); 547 if (erc == 0) { 548 (void) 549 tlm_entry_restored( 550 job_stats, 551 file_name, pos); 552 NDMP_LOG(LOG_DEBUG, 553 "restored %s -> %s", 554 nmp, 555 hardlink_target); 556 } 557 } else { 558 NDMP_LOG(LOG_DEBUG, 559 "no target for hardlink %s", 560 nmp); 561 } 562 563 name[0] = 0; 564 is_long_name = FALSE; 565 } 566 567 nm_end = 0; 568 longname[0] = 0; 569 lnk_end = 0; 570 longlink[0] = 0; 571 572 break; 573 } 574 /* otherwise fall through, restore like a normal file */ 575 /*FALLTHROUGH*/ 576 case LF_OLDNORMAL: 577 /* 578 * check for TAR's end-of-tape method 579 * of zero filled records. 580 */ 581 if (tar_hdr->th_name[0] == 0) { 582 break; 583 } 584 /* 585 * otherwise fall through, 586 * this is an old style normal file header 587 */ 588 /*FALLTHROUGH*/ 589 case LF_NORMAL: 590 case LF_CONTIG: 591 job_stats->js_files_so_far++; 592 if (*hugename != 0) { 593 (void) strlcpy(longname, hugename, 594 TLM_MAX_PATH_NAME); 595 } else if (*longname == 0) { 596 if (tar_hdr->th_name[0] != '/') { 597 /* 598 * check for old tar format, it 599 * does not have a leading "/" 600 */ 601 longname[0] = '/'; 602 longname[1] = 0; 603 (void) strlcat(longname, 604 tar_hdr->th_name, 605 TLM_MAX_PATH_NAME); 606 } else { 607 (void) strlcpy(longname, 608 tar_hdr->th_name, 609 TLM_MAX_PATH_NAME); 610 } 611 } 612 613 want_this_file = is_file_wanted(longname, sels, exls, 614 flags, &mchtype, &pos); 615 if (!want_this_file) { 616 nmp = NULL; 617 /* 618 * This means that DMA did not send us valid 619 * fh_info for the file in restore list. We 620 * use the file name entry in sels[] (ignore 621 * the name in the tar header) as restore 622 * target. 623 */ 624 if (DAR && (tar_hdr->th_linkflag == LF_LINK)) { 625 nmp = rs_darhl_new_name(rnp, name, 626 sels, &pos, longname); 627 628 if (nmp) { 629 want_this_file = TRUE; 630 mchtype = PM_EXACT; 631 } else { 632 break_flg = TRUE; 633 break; 634 } 635 } 636 } else { 637 nmp = rs_new_name(rnp, name, pos, longname); 638 if (!nmp) 639 want_this_file = FALSE; 640 } 641 642 if (nmp) 643 (void) strlcpy(parentlnk, nmp, strlen(nmp) + 1); 644 645 /* 646 * For a hardlink, even if it's not asked to be 647 * restored, we restore it to a temporary location, 648 * in case other links to the same file need to be 649 * restored later. 650 * 651 * The temp files are created in tmplink_dir, with 652 * names like ".tmphlrsnondar*". They are cleaned up 653 * at the completion of a restore. However, if a 654 * restore were interrupted, e.g. by a system reboot, 655 * they would have to be cleaned up manually in order 656 * for the disk space to be freed. 657 * 658 * If tmplink_dir is NULL, no temperorary files are 659 * created during a restore. This may result in some 660 * hardlinks not being restored during a partial 661 * restore. 662 */ 663 if (is_hardlink && !DAR && !want_this_file && !nmp) { 664 if (tmplink_dir) { 665 (void) snprintf(name, TLM_MAX_PATH_NAME, 666 "%s/%s_%d", tmplink_dir, 667 hardlink_tmp_name, 668 hardlink_tmp_idx); 669 nmp = name; 670 671 hardlink_tmp_idx++; 672 hardlink_tmp_file = 1; 673 want_this_file = TRUE; 674 NDMP_LOG(LOG_DEBUG, 675 "To restore temp hardlink file %s.", 676 nmp); 677 } else { 678 NDMP_LOG(LOG_DEBUG, 679 "No tmplink_dir specified."); 680 } 681 } 682 683 size_left = restore_file(&fp, nmp, file_size, 684 huge_size, acls, want_this_file, local_commands, 685 job_stats); 686 687 /* 688 * In the case of non-DAR, we have to record the first 689 * link for an inode that has multiple links. That's 690 * the only link with data records actually backed up. 691 * In this way, when we run into the other links, they 692 * will be treated as links, and we won't go to look 693 * for the data records to restore. This is not a 694 * problem for DAR, where DMA tells the tape where 695 * to locate the data records. 696 */ 697 if (is_hardlink && !DAR) { 698 if (hardlink_q_add(hardlink_q, hardlink_inode, 699 0, nmp, hardlink_tmp_file)) 700 NDMP_LOG(LOG_DEBUG, 701 "failed to add (%u, %s) to HL q", 702 hardlink_inode, nmp); 703 } 704 705 /* remove / reverse the temporary stuff */ 706 if (hardlink_tmp_file) { 707 nmp = NULL; 708 want_this_file = FALSE; 709 hardlink_tmp_file = 0; 710 } 711 712 /* 713 * Check if it is time to set the attribute 714 * of the restored directory 715 */ 716 while (nmp && ((bkpath = dtree_peek(stp)) != NULL)) { 717 if (strstr(nmp, bkpath)) 718 break; 719 720 (void) dtree_pop(stp); 721 } 722 723 NDMP_LOG(LOG_DEBUG, "sizeleft %s %d, %lld", longname, 724 size_left, huge_size); 725 726 if (size_left == -TLM_STOP) { 727 break_flg = TRUE; 728 rv = -1; 729 commands->tcs_reader = TLM_ABORT; 730 NDMP_LOG(LOG_DEBUG, "restoring [%s] failed", 731 longname); 732 break; 733 } 734 735 if (want_this_file) { 736 job_stats->js_bytes_total += file_size; 737 job_stats->js_files_total++; 738 } 739 740 huge_size -= file_size; 741 if (huge_size < 0) { 742 huge_size = 0; 743 } 744 if (size_left == 0 && huge_size == 0) { 745 if (PM_EXACT_OR_CHILD(mchtype)) { 746 (void) tlm_entry_restored(job_stats, 747 longname, pos); 748 749 /* 750 * Add an entry to hardlink_q to record 751 * this hardlink. 752 */ 753 if (is_hardlink) { 754 NDMP_LOG(LOG_DEBUG, 755 "Restored hardlink file %s", 756 nmp); 757 758 if (DAR) { 759 (void) hardlink_q_add( 760 hardlink_q, 761 hardlink_inode, 0, 762 nmp, 0); 763 } 764 } 765 } 766 767 nm_end = 0; 768 longname[0] = 0; 769 lnk_end = 0; 770 longlink[0] = 0; 771 hugename[0] = 0; 772 name[0] = 0; 773 is_long_name = FALSE; 774 } 775 break; 776 case LF_XATTR: 777 file_name = (*longname == 0) ? tar_hdr->th_name : 778 longname; 779 780 size_left = restore_xattr_hdr(&fp, parentlnk, 781 file_name, file_size, acls, local_commands, 782 job_stats); 783 784 break; 785 case LF_SYMLINK: 786 file_name = (*longname == 0) ? tar_hdr->th_name : 787 longname; 788 link_name = (*longlink == 0) ? 789 tar_hdr->th_linkname : longlink; 790 NDMP_LOG(LOG_DEBUG, "file_name[%s]", file_name); 791 NDMP_LOG(LOG_DEBUG, "link_name[%s]", link_name); 792 if (is_file_wanted(file_name, sels, exls, flags, 793 &mchtype, &pos)) { 794 nmp = rs_new_name(rnp, name, pos, file_name); 795 if (nmp) { 796 erc = create_sym_link(nmp, link_name, 797 acls, job_stats); 798 if (erc == 0 && 799 PM_EXACT_OR_CHILD(mchtype)) 800 (void) tlm_entry_restored( 801 job_stats, file_name, pos); 802 name[0] = 0; 803 } 804 } 805 nm_end = 0; 806 longname[0] = 0; 807 lnk_end = 0; 808 longlink[0] = 0; 809 break; 810 case LF_DIR: 811 file_name = *longname == 0 ? tar_hdr->th_name : 812 longname; 813 if (is_file_wanted(file_name, sels, exls, flags, 814 &mchtype, &pos)) { 815 nmp = rs_new_name(rnp, name, pos, file_name); 816 if (nmp && mchtype != PM_PARENT) { 817 (void) strlcpy(parentlnk, nmp, 818 strlen(nmp)); 819 erc = create_directory(nmp, job_stats); 820 if (erc == 0 && 821 PM_EXACT_OR_CHILD(mchtype)) 822 (void) tlm_entry_restored( 823 job_stats, file_name, pos); 824 /* 825 * Check if it is time to set 826 * the attribute of the restored 827 * directory 828 */ 829 while ((bkpath = dtree_peek(stp)) 830 != NULL) { 831 if (strstr(nmp, bkpath)) 832 break; 833 (void) dtree_pop(stp); 834 } 835 836 (void) dtree_push(stp, nmp, acls); 837 name[0] = 0; 838 } 839 } 840 nm_end = 0; 841 longname[0] = 0; 842 lnk_end = 0; 843 longlink[0] = 0; 844 break; 845 case LF_FIFO: 846 file_name = *longname == 0 ? tar_hdr->th_name : 847 longname; 848 if (is_file_wanted(file_name, sels, exls, flags, 849 &mchtype, &pos)) { 850 nmp = rs_new_name(rnp, name, pos, file_name); 851 if (nmp) { 852 erc = create_fifo(nmp, acls); 853 if (erc == 0 && 854 PM_EXACT_OR_CHILD(mchtype)) 855 (void) tlm_entry_restored( 856 job_stats, file_name, pos); 857 name[0] = 0; 858 } 859 } 860 nm_end = 0; 861 longname[0] = 0; 862 lnk_end = 0; 863 longlink[0] = 0; 864 break; 865 case LF_LONGLINK: 866 file_size = min(file_size, TLM_MAX_PATH_NAME - lnk_end); 867 file_size = max(0, file_size); 868 size_left = get_long_name(lib, drv, file_size, longlink, 869 &lnk_end, local_commands); 870 871 if (size_left != 0) 872 NDMP_LOG(LOG_DEBUG, 873 "fsize %d sleft %d lnkend %d", 874 file_size, size_left, lnk_end); 875 break; 876 case LF_LONGNAME: 877 file_size = min(file_size, TLM_MAX_PATH_NAME - nm_end); 878 file_size = max(0, file_size); 879 size_left = get_long_name(lib, drv, file_size, longname, 880 &nm_end, local_commands); 881 882 if (size_left != 0) 883 NDMP_LOG(LOG_DEBUG, 884 "fsize %d sleft %d nmend %d", 885 file_size, size_left, nm_end); 886 is_long_name = TRUE; 887 break; 888 case LF_ACL: 889 size_left = load_acl_info(lib, drv, file_size, acls, 890 &acl_spot, local_commands); 891 break; 892 case LF_VOLHDR: 893 break; 894 case LF_HUMONGUS: 895 (void) memset(hugename, 0, TLM_MAX_PATH_NAME); 896 (void) get_humongus_file_header(lib, drv, file_size, 897 &huge_size, hugename, local_commands); 898 break; 899 default: 900 break; 901 902 } 903 904 /* 905 * If the restore is running using DAR we should check for 906 * long file names and HUGE file sizes. 907 */ 908 if (DAR && tar_hdr->th_linkflag != LF_ACL && 909 tar_hdr->th_linkflag != LF_XATTR && 910 !huge_size && !is_long_name) 911 dar_recovered = 1; 912 } 913 914 /* 915 * tear down 916 */ 917 if (fp != 0) { 918 (void) close(fp); 919 } 920 while (dtree_pop(stp) != -1) 921 ; 922 cstack_delete(stp); 923 free(acls); 924 free(longname); 925 free(parentlnk); 926 free(longlink); 927 free(hugename); 928 free(name); 929 return (rv); 930 } 931 932 /* 933 * Main file restore function for tar (should run as a thread) 934 */ 935 int 936 tar_getfile(tlm_backup_restore_arg_t *argp) 937 { 938 tlm_job_stats_t *job_stats; 939 char **sels; /* list of files desired */ 940 char **exls; /* list of files not wanted */ 941 char *dir; /* where to restore the files */ 942 char job[TLM_MAX_BACKUP_JOB_NAME+1]; 943 /* the restore job name */ 944 int erc; /* error return codes */ 945 int flags; 946 struct rs_name_maker rn; 947 tlm_commands_t *commands; 948 tlm_cmd_t *local_commands; 949 char *list = NULL; 950 951 commands = argp->ba_commands; 952 local_commands = argp->ba_cmd; 953 954 flags = 0; 955 956 dir = ndmp_malloc(TLM_MAX_PATH_NAME); 957 if (dir == NULL) { 958 local_commands->tc_reader = TLM_STOP; 959 (void) pthread_barrier_wait(&argp->ba_barrier); 960 return (-1); 961 } 962 963 (void) strlcpy(job, argp->ba_job, TLM_MAX_BACKUP_JOB_NAME+1); 964 (void) strlcpy(dir, argp->ba_dir, TLM_MAX_PATH_NAME); 965 966 flags |= RSFLG_OVR_ALWAYS; 967 flags |= RSFLG_IGNORE_CASE; 968 969 /* 970 * do not test for "dir" having no string, since that 971 * is a legal condition. Restore to origional location 972 * will not have a restore directory. 973 */ 974 if (*job == '\0') { 975 NDMP_LOG(LOG_DEBUG, "No job defined"); 976 local_commands->tc_reader = TLM_STOP; 977 free(dir); 978 (void) pthread_barrier_wait(&argp->ba_barrier); 979 return (-1); 980 } 981 982 sels = argp->ba_sels; 983 if (sels == NULL) { 984 local_commands->tc_reader = TLM_STOP; 985 free(dir); 986 (void) pthread_barrier_wait(&argp->ba_barrier); 987 return (-1); 988 } 989 exls = &list; 990 991 tlm_log_list("selections", sels); 992 tlm_log_list("exclusions", exls); 993 994 if (wildcard_enabled()) 995 flags |= RSFLG_MATCH_WCARD; 996 997 local_commands->tc_ref++; 998 commands->tcs_writer_count++; 999 1000 /* 1001 * let the launcher continue 1002 */ 1003 (void) pthread_barrier_wait(&argp->ba_barrier); 1004 1005 job_stats = tlm_ref_job_stats(job); 1006 1007 rn.rn_fp = catnames; 1008 rn.rn_nlp = dir; 1009 1010 /* 1011 * work 1012 */ 1013 NDMP_LOG(LOG_DEBUG, "start restore job %s", job); 1014 erc = tar_getdir(commands, local_commands, job_stats, &rn, 1, 1, 1015 sels, exls, flags, 0, NULL); 1016 1017 /* 1018 * teardown 1019 */ 1020 NDMP_LOG(LOG_DEBUG, "end restore job %s", job); 1021 tlm_un_ref_job_stats(job); 1022 tlm_release_list(sels); 1023 tlm_release_list(exls); 1024 1025 commands->tcs_writer_count--; 1026 local_commands->tc_reader = TLM_STOP; 1027 tlm_release_reader_writer_ipc(local_commands); 1028 free(dir); 1029 return (erc); 1030 } 1031 1032 /* 1033 * Creates the directories all the way down to the 1034 * end if they dont exist 1035 */ 1036 int 1037 make_dirs(char *dir) 1038 { 1039 char c; 1040 char *cp, *end; 1041 struct stat64 st; 1042 1043 cp = dir; 1044 cp += strspn(cp, "/"); 1045 end = dir + strlen(dir); 1046 do { 1047 if (*cp == '\0' || *cp == '/') { 1048 c = *cp; 1049 *cp = '\0'; 1050 if (lstat64(dir, &st) < 0) 1051 if (mkdir(dir, 0777) < 0) { 1052 NDMP_LOG(LOG_DEBUG, "Error %d" 1053 " creating directory %s", 1054 errno, dir); 1055 *cp = c; 1056 return (-1); 1057 } 1058 1059 *cp = c; 1060 } 1061 } while (++cp <= end); 1062 1063 return (0); 1064 } 1065 1066 /* 1067 * Creates the directories leading to the given path 1068 */ 1069 int 1070 mkbasedir(char *path) 1071 { 1072 int rv; 1073 char *cp; 1074 struct stat64 st; 1075 1076 if (!path || !*path) { 1077 NDMP_LOG(LOG_DEBUG, "Invalid argument"); 1078 return (-1); 1079 } 1080 1081 cp = strrchr(path, '/'); 1082 if (cp) 1083 *cp = '\0'; 1084 rv = lstat64(path, &st); 1085 if (rv < 0) /* need new directories */ 1086 rv = make_dirs(path); 1087 if (cp) 1088 *cp = '/'; 1089 1090 return (rv); 1091 } 1092 1093 1094 /* 1095 * read the file off the tape back onto disk 1096 */ 1097 static long 1098 restore_file(int *fp, 1099 char *real_name, 1100 long size, 1101 longlong_t huge_size, 1102 tlm_acls_t *acls, 1103 boolean_t want_this_file, 1104 tlm_cmd_t *local_commands, 1105 tlm_job_stats_t *job_stats) 1106 { 1107 struct stat64 attr; 1108 1109 if (!real_name) { 1110 if (want_this_file) { 1111 NDMP_LOG(LOG_DEBUG, "No file name but wanted!"); 1112 want_this_file = FALSE; 1113 } 1114 } else 1115 NDMP_LOG(LOG_DEBUG, "new file[%s]", real_name); 1116 1117 /* 1118 * OK, some FM is creeping in here ... 1119 * int *fp is used to keep the 1120 * backup file channel open through 1121 * the interruption of EOT and 1122 * processing the headers of the 1123 * next tape. So, if *fp is zero 1124 * then no file is open yet and all 1125 * is normal. If *fp has a number 1126 * then we are returning after an 1127 * EOT break. 1128 * 1129 * *fp is now also open for HUGE files 1130 * that are put back in sections. 1131 */ 1132 1133 if (*fp == 0 && want_this_file) { 1134 int erc_stat; 1135 1136 if (mkbasedir(real_name) < 0) 1137 job_stats->js_errors++; 1138 1139 erc_stat = stat64(real_name, (struct stat64 *)&attr); 1140 if (erc_stat < 0) { 1141 /*EMPTY*/ 1142 /* new file */ 1143 } else if (acls->acl_overwrite) { 1144 /*EMPTY*/ 1145 /* take this file no matter what */ 1146 } else if (acls->acl_update) { 1147 if (attr.st_mtime < acls->acl_attr.st_mtime) { 1148 /*EMPTY*/ 1149 /* tape is newer */ 1150 } else { 1151 /* disk file is newer */ 1152 want_this_file = FALSE; 1153 } 1154 } else { 1155 /* 1156 * no overwrite, no update, 1157 * do not ever replace old files. 1158 */ 1159 want_this_file = TRUE; 1160 } 1161 if (want_this_file) { 1162 1163 *fp = open(real_name, O_CREAT | O_TRUNC | O_WRONLY, 1164 S_IRUSR | S_IWUSR); 1165 if (*fp == -1) { 1166 NDMP_LOG(LOG_ERR, 1167 "Could not open %s for restore.", 1168 real_name); 1169 NDMP_LOG(LOG_DEBUG, 1170 "fp=%d err=%d ", *fp, errno); 1171 job_stats->js_errors++; 1172 want_this_file = FALSE; 1173 /* 1174 * we cannot return here, 1175 * the file is still on 1176 * the tape and must be 1177 * skipped over. 1178 */ 1179 } 1180 } 1181 (void) strlcpy(local_commands->tc_file_name, real_name, 1182 TLM_MAX_PATH_NAME); 1183 } 1184 1185 /* 1186 * this is the size left in the next segment 1187 */ 1188 huge_size -= size; 1189 1190 /* 1191 * work 1192 */ 1193 while (size > 0 && local_commands->tc_writer == TLM_RESTORE_RUN) { 1194 int actual_size; 1195 int error; 1196 char *rec; 1197 int write_size; 1198 1199 /* 1200 * Use bytes_in_file field to tell reader the amount 1201 * of data still need to be read for this file. 1202 */ 1203 job_stats->js_bytes_in_file = size; 1204 1205 error = 0; 1206 rec = get_read_buffer(size, &error, &actual_size, 1207 local_commands); 1208 if (actual_size <= 0) { 1209 NDMP_LOG(LOG_DEBUG, 1210 "RESTORE WRITER> error %d, actual_size %d", 1211 error, actual_size); 1212 1213 /* no more data for this file for now */ 1214 job_stats->js_bytes_in_file = 0; 1215 1216 return (size); 1217 } else if (error) { 1218 NDMP_LOG(LOG_DEBUG, "Error %d in file [%s]", 1219 error, local_commands->tc_file_name); 1220 break; 1221 } else { 1222 write_size = min(size, actual_size); 1223 if (want_this_file) { 1224 write_size = write(*fp, rec, write_size); 1225 } 1226 NS_ADD(wdisk, write_size); 1227 NS_INC(wfile); 1228 size -= write_size; 1229 } 1230 } 1231 1232 /* no more data for this file for now */ 1233 job_stats->js_bytes_in_file = 0; 1234 1235 /* 1236 * teardown 1237 */ 1238 if (*fp != 0 && huge_size <= 0) { 1239 (void) close(*fp); 1240 *fp = 0; 1241 set_acl(real_name, acls); 1242 } 1243 return (0); 1244 } 1245 1246 /* 1247 * Set the extended attributes file attribute 1248 */ 1249 static void 1250 set_xattr(int fd, struct stat64 st) 1251 { 1252 struct timeval times[2]; 1253 1254 times[0].tv_sec = st.st_atime; 1255 times[1].tv_sec = st.st_mtime; 1256 1257 (void) fchmod(fd, st.st_mode); 1258 (void) fchown(fd, st.st_uid, st.st_gid); 1259 (void) futimesat(fd, ".", times); 1260 } 1261 1262 /* 1263 * Read the system attribute file in a single buffer to write 1264 * it as a single write. A partial write to system attribute would 1265 * cause an EINVAL on write. 1266 */ 1267 static char * 1268 get_read_one_buf(char *rec, int actual_size, int size, int *error, 1269 tlm_cmd_t *lc) 1270 { 1271 char *buf, *p; 1272 int read_size; 1273 int len; 1274 1275 if (actual_size > size) 1276 return (rec); 1277 1278 buf = ndmp_malloc(size); 1279 if (buf == NULL) { 1280 *error = ENOMEM; 1281 return (NULL); 1282 } 1283 (void) memcpy(buf, rec, actual_size); 1284 rec = buf; 1285 buf += actual_size; 1286 while (actual_size < size) { 1287 p = get_read_buffer(size - actual_size, error, &read_size, lc); 1288 len = min(size - actual_size, read_size); 1289 (void) memcpy(buf, p, len); 1290 actual_size += len; 1291 buf += len; 1292 } 1293 return (rec); 1294 } 1295 1296 1297 /* 1298 * read the extended attribute header and write 1299 * it to the file 1300 */ 1301 static long 1302 restore_xattr_hdr(int *fp, 1303 char *name, 1304 char *fname, 1305 long size, 1306 tlm_acls_t *acls, 1307 tlm_cmd_t *local_commands, 1308 tlm_job_stats_t *job_stats) 1309 { 1310 tlm_tar_hdr_t *tar_hdr; 1311 struct xattr_hdr *xhdr; 1312 struct xattr_buf *xbuf; 1313 int namelen; 1314 char *xattrname; 1315 int actual_size; 1316 int error; 1317 1318 if (!fname) { 1319 NDMP_LOG(LOG_DEBUG, "No file name but wanted!"); 1320 } else { 1321 NDMP_LOG(LOG_DEBUG, "new xattr[%s]", fname); 1322 } 1323 1324 error = 0; 1325 xhdr = (struct xattr_hdr *)get_read_buffer(size, &error, 1326 &actual_size, local_commands); 1327 if (xhdr == NULL || error != 0) { 1328 NDMP_LOG(LOG_DEBUG, 1329 "Could not read xattr [%s:%s] for restore. ", 1330 name, fname); 1331 job_stats->js_errors++; 1332 return (0); 1333 } 1334 1335 /* Check extended attribute header */ 1336 if (strcmp(xhdr->h_version, XATTR_ARCH_VERS) != 0) { 1337 NDMP_LOG(LOG_DEBUG, 1338 "Unrecognized header format [%s]", xhdr->h_version); 1339 return (0); 1340 } 1341 xbuf = (struct xattr_buf *)(((char *)xhdr) + sizeof (struct xattr_hdr)); 1342 1343 (void) sscanf(xbuf->h_namesz, "%7d", &namelen); 1344 xattrname = xbuf->h_names + strlen(xbuf->h_names) + 1; 1345 1346 if (*fp == 0) { 1347 int fd; 1348 1349 fd = attropen(name, xattrname, O_CREAT | O_RDWR, 0755); 1350 if (fd == -1) { 1351 NDMP_LOG(LOG_DEBUG, 1352 "Could not open xattr [%s:%s] for restore err=%d.", 1353 name, xattrname, errno); 1354 job_stats->js_errors++; 1355 return (0); 1356 } 1357 (void) strlcpy(local_commands->tc_file_name, xattrname, 1358 TLM_MAX_PATH_NAME); 1359 *fp = fd; 1360 } 1361 1362 /* Get the actual extended attribute file */ 1363 tar_hdr = (tlm_tar_hdr_t *)get_read_buffer(sizeof (*tar_hdr), 1364 &error, &actual_size, local_commands); 1365 if (tar_hdr == NULL || error != 0) { 1366 NDMP_LOG(LOG_DEBUG, 1367 "Could not read xattr data [%s:%s] for restore. ", 1368 fname, xattrname); 1369 job_stats->js_errors++; 1370 return (0); 1371 } 1372 acls->acl_attr.st_mode = oct_atoi(tar_hdr->th_mode); 1373 acls->acl_attr.st_size = oct_atoi(tar_hdr->th_size); 1374 acls->acl_attr.st_uid = oct_atoi(tar_hdr->th_uid); 1375 acls->acl_attr.st_gid = oct_atoi(tar_hdr->th_gid); 1376 acls->acl_attr.st_mtime = oct_atoi(tar_hdr->th_mtime); 1377 1378 NDMP_LOG(LOG_DEBUG, "xattr_hdr: %s size %d mode %06o uid %d gid %d", 1379 xattrname, acls->acl_attr.st_size, acls->acl_attr.st_mode, 1380 acls->acl_attr.st_uid, acls->acl_attr.st_gid); 1381 1382 size = acls->acl_attr.st_size; 1383 while (size > 0 && local_commands->tc_writer == TLM_RESTORE_RUN) { 1384 char *rec; 1385 int write_size; 1386 int sysattr_write = 0; 1387 1388 error = 0; 1389 rec = get_read_buffer(size, &error, &actual_size, 1390 local_commands); 1391 1392 if ((actual_size < size) && sysattr_rw(xattrname)) { 1393 rec = get_read_one_buf(rec, actual_size, size, &error, 1394 local_commands); 1395 if (rec == NULL) { 1396 NDMP_LOG(LOG_DEBUG, "Error %d in file [%s]", 1397 error, xattrname); 1398 return (size); 1399 } 1400 actual_size = size; 1401 sysattr_write = 1; 1402 } 1403 if (actual_size <= 0) { 1404 NDMP_LOG(LOG_DEBUG, 1405 "RESTORE WRITER> error %d, actual_size %d", 1406 error, actual_size); 1407 1408 return (size); 1409 } else if (error) { 1410 NDMP_LOG(LOG_DEBUG, "Error %d in file [%s]", 1411 error, local_commands->tc_file_name); 1412 break; 1413 } else { 1414 write_size = min(size, actual_size); 1415 if ((write_size = write(*fp, rec, write_size)) < 0) { 1416 if (sysattr_write) 1417 free(rec); 1418 1419 break; 1420 } 1421 1422 NS_ADD(wdisk, write_size); 1423 NS_INC(wfile); 1424 size -= write_size; 1425 } 1426 if (sysattr_write) 1427 free(rec); 1428 } 1429 1430 if (*fp != 0) { 1431 set_xattr(*fp, acls->acl_attr); 1432 (void) close(*fp); 1433 *fp = 0; 1434 } 1435 return (0); 1436 } 1437 1438 /* 1439 * Match the name with the list 1440 */ 1441 static int 1442 exact_find(char *name, char **list) 1443 { 1444 boolean_t found; 1445 int i; 1446 char *cp; 1447 1448 found = FALSE; 1449 for (i = 0; *list != NULL; list++, i++) { 1450 cp = *list + strspn(*list, "/"); 1451 if (match(cp, name)) { 1452 found = TRUE; 1453 NDMP_LOG(LOG_DEBUG, "exact_find> found[%s]", cp); 1454 break; 1455 } 1456 } 1457 1458 return (found); 1459 } 1460 1461 /* 1462 * On error, return FALSE and prevent restoring(probably) unwanted data. 1463 */ 1464 static int 1465 is_parent(char *parent, char *child, int flags) 1466 { 1467 char tmp[TLM_MAX_PATH_NAME]; 1468 boolean_t rv; 1469 1470 if (IS_SET(flags, RSFLG_MATCH_WCARD)) { 1471 if (!tlm_cat_path(tmp, parent, "*")) { 1472 NDMP_LOG(LOG_DEBUG, 1473 "is_parent> path too long [%s]", parent); 1474 rv = FALSE; 1475 } else 1476 rv = (match(tmp, child) != 0) ? TRUE : FALSE; 1477 } else { 1478 if (!tlm_cat_path(tmp, parent, "/")) { 1479 NDMP_LOG(LOG_DEBUG, 1480 "is_parent> path too long [%s]", parent); 1481 rv = FALSE; 1482 } else 1483 rv = (strncmp(tmp, child, strlen(tmp)) == 0) ? 1484 TRUE : FALSE; 1485 } 1486 1487 return (rv); 1488 } 1489 1490 /* 1491 * Used to match the filename inside the list 1492 */ 1493 static boolean_t 1494 strexactcmp(char *s, char *t) 1495 { 1496 return ((strcmp(s, t) == 0) ? TRUE : FALSE); 1497 } 1498 1499 /* 1500 * Check if the file is needed to be restored 1501 */ 1502 static boolean_t 1503 is_file_wanted(char *name, 1504 char **sels, 1505 char **exls, 1506 int flags, 1507 int *mchtype, 1508 int *pos) 1509 { 1510 char *p_sel; 1511 char *uc_name, *retry, *namep; 1512 boolean_t found; 1513 int i; 1514 name_match_fp_t *cmp_fp; 1515 1516 if (name == NULL || sels == NULL || exls == NULL) 1517 return (FALSE); 1518 1519 found = FALSE; 1520 if (mchtype != NULL) 1521 *mchtype = PM_NONE; 1522 if (pos != NULL) 1523 *pos = 0; 1524 1525 /* 1526 * For empty selection, restore everything 1527 */ 1528 if (*sels == NULL || **sels == '\0') { 1529 NDMP_LOG(LOG_DEBUG, "is_file_wanted: Restore all"); 1530 return (TRUE); 1531 } 1532 1533 uc_name = ndmp_malloc(TLM_MAX_PATH_NAME); 1534 retry = ndmp_malloc(TLM_MAX_PATH_NAME); 1535 if (uc_name == NULL || retry == NULL) { 1536 free(uc_name); 1537 free(retry); 1538 return (FALSE); 1539 } 1540 1541 if (IS_SET(flags, RSFLG_MATCH_WCARD)) 1542 cmp_fp = match; 1543 else 1544 cmp_fp = strexactcmp; 1545 1546 namep = name + strspn(name, "/"); 1547 if (IS_SET(flags, RSFLG_IGNORE_CASE)) { 1548 (void) strlcpy(uc_name, namep, TLM_MAX_PATH_NAME); 1549 (void) strupr(uc_name); 1550 namep = uc_name; 1551 } 1552 NDMP_LOG(LOG_DEBUG, "is_file_wanted> flg: 0x%x name: [%s]", 1553 flags, name); 1554 1555 for (i = 0; *sels != NULL; sels++, i++) { 1556 p_sel = *sels + strspn(*sels, "/"); 1557 1558 /* 1559 * Try exact match. 1560 */ 1561 if ((*cmp_fp)(p_sel, namep)) { 1562 NDMP_LOG(LOG_DEBUG, "match1> pos: %d [%s][%s]", 1563 i, p_sel, name); 1564 found = TRUE; 1565 if (mchtype != NULL) 1566 *mchtype = PM_EXACT; 1567 break; 1568 } 1569 /* 1570 * Try "entry/" and the current selection. The 1571 * current selection may be something like "<something>/". 1572 */ 1573 (void) tlm_cat_path(retry, namep, "/"); 1574 if ((*cmp_fp)(p_sel, retry)) { 1575 NDMP_LOG(LOG_DEBUG, "match2> pos %d [%s][%s]", 1576 i, p_sel, name); 1577 found = TRUE; 1578 if (mchtype != NULL) 1579 *mchtype = PM_EXACT; 1580 break; 1581 } 1582 /* 1583 * If the following check returns true it means that the 1584 * 'name' is an entry below the 'p_sel' hierarchy. 1585 */ 1586 if (is_parent(p_sel, namep, flags)) { 1587 NDMP_LOG(LOG_DEBUG, "parent1> pos %d [%s][%s]", 1588 i, p_sel, name); 1589 found = TRUE; 1590 if (mchtype != NULL) 1591 *mchtype = PM_CHILD; 1592 break; 1593 } 1594 1595 /* 1596 * There is a special case for parent directories of a 1597 * selection. If 'p_sel' is something like "*d1", the 1598 * middle directories of the final entry can't be determined 1599 * until the final entry matches with 'p_sel'. At that 1600 * time the middle directories of the entry have been passed 1601 * and they can't be restored. 1602 */ 1603 if (is_parent(namep, p_sel, flags)) { 1604 NDMP_LOG(LOG_DEBUG, "parent2> pos %d [%s][%s]", 1605 i, p_sel, name); 1606 found = TRUE; 1607 if (mchtype != NULL) 1608 *mchtype = PM_PARENT; 1609 break; 1610 } 1611 } 1612 1613 /* Check for exclusions. */ 1614 if (found && exact_find(namep, exls)) { 1615 if (mchtype != NULL) 1616 *mchtype = PM_NONE; 1617 found = FALSE; 1618 } 1619 if (found && pos != NULL) 1620 *pos = i; 1621 1622 free(uc_name); 1623 free(retry); 1624 return (found); 1625 } 1626 1627 /* 1628 * Read the specified amount data into the buffer. Detects EOT or EOF 1629 * during read. 1630 * 1631 * Returns the number of bytes actually read. On error returns -1. 1632 */ 1633 static int 1634 input_mem(int l, 1635 int d, 1636 tlm_cmd_t *lcmds, 1637 char *mem, 1638 int len) 1639 { 1640 int err; 1641 int toread, actual_size, rec_size; 1642 char *rec; 1643 1644 if (l <= 0 || d <= 0 || !lcmds || !mem) { 1645 NDMP_LOG(LOG_DEBUG, "Invalid argument"); 1646 return (-1); 1647 } 1648 1649 toread = len; 1650 while (toread > 0) { 1651 rec = get_read_buffer(toread, &err, &actual_size, lcmds); 1652 if (actual_size <= 0) { 1653 NDMP_LOG(LOG_DEBUG, "err %d act_size %d detected", 1654 err, actual_size); 1655 break; 1656 } else if (err) { 1657 NDMP_LOG(LOG_DEBUG, "error %d reading data", err); 1658 return (-1); 1659 } 1660 rec_size = min(actual_size, toread); 1661 (void) memcpy(mem, rec, rec_size); 1662 mem += rec_size; 1663 toread -= rec_size; 1664 } 1665 1666 return (len - toread); 1667 } 1668 1669 /* 1670 * pick up the name and size of a HUGE file 1671 */ 1672 static int 1673 get_humongus_file_header(int lib, 1674 int drv, 1675 long recsize, 1676 longlong_t *size, 1677 char *name, 1678 tlm_cmd_t *local_commands) 1679 { 1680 char *p_record, *value; 1681 int rv; 1682 1683 NDMP_LOG(LOG_DEBUG, "HUGE Record found: %d", recsize); 1684 1685 rv = 0; 1686 if (recsize == 0) { 1687 /* 1688 * The humongus_file_header was written in a 1689 * RECORDSIZE block and the header.size field of this 1690 * record was 0 before this fix. For backward compatiblity 1691 * read only one RECORDSIZE-size block if the header.size 1692 * field is 0. Otherwise the header.size field should show 1693 * the length of the data of this header. 1694 */ 1695 NDMP_LOG(LOG_DEBUG, "Old HUGE record found"); 1696 recsize = RECORDSIZE; 1697 } 1698 1699 if (input_mem(lib, drv, local_commands, name, recsize) != recsize) { 1700 rv = -1; 1701 *size = 0; 1702 *name = '\0'; 1703 NDMP_LOG(LOG_DEBUG, "Error reading a HUGE file name"); 1704 } else { 1705 NDMP_LOG(LOG_DEBUG, "HUGE [%s]", name); 1706 1707 p_record = name; 1708 value = parse(&p_record, " "); 1709 *size = atoll(value); 1710 /* 1711 * Note: Since the backed up names are not longer than 1712 * NAME_MAX and the buffer passed to us is 1713 * TLM_MAX_PATH_NAME, it should be safe to use strlcpy 1714 * without check on the buffer size. 1715 */ 1716 (void) strlcpy(name, p_record, TLM_MAX_PATH_NAME); 1717 } 1718 1719 NDMP_LOG(LOG_DEBUG, "HUGE Record %lld [%s]", *size, name); 1720 1721 return (rv); 1722 } 1723 1724 /* 1725 * pick up the long name from the special tape file 1726 */ 1727 static int 1728 get_long_name(int lib, 1729 int drv, 1730 long recsize, 1731 char *name, 1732 long *buf_spot, 1733 tlm_cmd_t *local_commands) 1734 { 1735 int nread; 1736 1737 NDMP_LOG(LOG_DEBUG, "LONGNAME Record found rs %d bs %d", recsize, 1738 *buf_spot); 1739 1740 if (*buf_spot < 0) 1741 *buf_spot = 0; 1742 1743 nread = input_mem(lib, drv, local_commands, name + *buf_spot, 1744 recsize); 1745 if (nread < 0) { 1746 nread = recsize; /* return 0 as size left */ 1747 name[*buf_spot] = '\0'; 1748 NDMP_LOG(LOG_ERR, "Error %d reading a long file name %s.", 1749 nread, name); 1750 } else { 1751 *buf_spot += nread; 1752 name[*buf_spot] = '\0'; 1753 NDMP_LOG(LOG_DEBUG, "LONGNAME [%s]", name); 1754 } 1755 1756 return (recsize - nread); 1757 } 1758 1759 /* 1760 * create a new directory 1761 */ 1762 static int 1763 create_directory(char *dir, tlm_job_stats_t *job_stats) 1764 { 1765 struct stat64 attr; 1766 char *p; 1767 char temp; 1768 int erc; 1769 1770 /* 1771 * Make sure all directories in this path exist, create them if 1772 * needed. 1773 */ 1774 NDMP_LOG(LOG_DEBUG, "new dir[%s]", dir); 1775 1776 erc = 0; 1777 p = &dir[1]; 1778 do { 1779 temp = *p; 1780 if (temp == '/' || temp == 0) { 1781 *p = 0; 1782 if (stat64(dir, &attr) < 0) { 1783 erc = mkdir(dir, 0777); 1784 if (erc < 0) { 1785 job_stats->js_errors++; 1786 NDMP_LOG(LOG_DEBUG, 1787 "Could not create directory %s", 1788 dir); 1789 break; 1790 } 1791 } 1792 *p = temp; 1793 } 1794 p++; 1795 } while (temp != 0); 1796 1797 return (erc); 1798 } 1799 1800 /* 1801 * create a new hardlink 1802 */ 1803 static int 1804 create_hard_link(char *name_old, char *name_new, 1805 tlm_acls_t *acls, tlm_job_stats_t *job_stats) 1806 { 1807 int erc; 1808 1809 if (mkbasedir(name_new)) { 1810 NDMP_LOG(LOG_DEBUG, "faile to make base dir for [%s]", 1811 name_new); 1812 1813 return (-1); 1814 } 1815 1816 erc = link(name_old, name_new); 1817 if (erc) { 1818 job_stats->js_errors++; 1819 NDMP_LOG(LOG_DEBUG, "error %d (errno %d) hardlink [%s] to [%s]", 1820 erc, errno, name_new, name_old); 1821 } else { 1822 set_acl(name_new, acls); 1823 } 1824 return (erc); 1825 } 1826 1827 /* 1828 * create a new symlink 1829 */ 1830 /*ARGSUSED*/ 1831 static int 1832 create_sym_link(char *dst, char *target, tlm_acls_t *acls, 1833 tlm_job_stats_t *job_satats) 1834 { 1835 int erc; 1836 1837 if (mkbasedir(dst) < 0) 1838 return (-1); 1839 1840 erc = symlink(target, dst); 1841 if (erc) { 1842 job_satats->js_errors++; 1843 NDMP_LOG(LOG_DEBUG, "error %d (errno %d) softlink [%s] to [%s]", 1844 erc, errno, dst, target); 1845 } else { 1846 set_acl(dst, acls); 1847 } 1848 1849 return (erc); 1850 } 1851 1852 /* 1853 * create a new FIFO 1854 */ 1855 static int 1856 create_fifo(char *name, tlm_acls_t *acls) 1857 { 1858 (void) mknod(name, 0777 + S_IFIFO, 0); 1859 set_acl(name, acls); 1860 return (0); 1861 } 1862 1863 /* 1864 * read in the ACLs for the next file 1865 */ 1866 static long 1867 load_acl_info(int lib, 1868 int drv, 1869 long file_size, 1870 tlm_acls_t *acls, 1871 long *acl_spot, 1872 tlm_cmd_t *local_commands) 1873 { 1874 char *bp; 1875 int nread; 1876 1877 /* 1878 * If the ACL is spanned on tapes, then the acl_spot should NOT be 1879 * 0 on next calls to this function to read the rest of the ACL 1880 * on next tapes. 1881 */ 1882 if (*acl_spot == 0) { 1883 (void) memset(acls, 0, sizeof (tlm_acls_t)); 1884 } 1885 1886 bp = ((char *)&acls->acl_info) + *acl_spot; 1887 nread = input_mem(lib, drv, local_commands, (void *)bp, file_size); 1888 if (nread < 0) { 1889 *acl_spot = 0; 1890 (void) memset(acls, 0, sizeof (tlm_acls_t)); 1891 NDMP_LOG(LOG_DEBUG, "Error reading ACL data"); 1892 return (0); 1893 } 1894 *acl_spot += nread; 1895 acls->acl_non_trivial = TRUE; 1896 1897 return (file_size - nread); 1898 } 1899 1900 static int 1901 ndmp_set_eprivs_least(void) 1902 { 1903 priv_set_t *priv_set; 1904 1905 if ((priv_set = priv_allocset()) == NULL) { 1906 NDMP_LOG(LOG_ERR, "Out of memory."); 1907 return (-1); 1908 } 1909 priv_emptyset(priv_set); 1910 (void) priv_addset(priv_set, "basic"); 1911 (void) priv_addset(priv_set, "proc_audit"); 1912 (void) priv_addset(priv_set, "proc_setid"); 1913 (void) priv_addset(priv_set, "proc_owner"); 1914 (void) priv_addset(priv_set, "file_chown"); 1915 (void) priv_addset(priv_set, "file_chown_self"); 1916 (void) priv_addset(priv_set, "file_dac_read"); 1917 (void) priv_addset(priv_set, "file_dac_search"); 1918 (void) priv_addset(priv_set, "file_dac_write"); 1919 (void) priv_addset(priv_set, "file_owner"); 1920 (void) priv_addset(priv_set, "file_setid"); 1921 (void) priv_addset(priv_set, "sys_linkdir"); 1922 (void) priv_addset(priv_set, "sys_devices"); 1923 (void) priv_addset(priv_set, "sys_mount"); 1924 (void) priv_addset(priv_set, "sys_config"); 1925 1926 if (setppriv(PRIV_SET, PRIV_EFFECTIVE, priv_set) == -1) { 1927 NDMP_LOG(LOG_ERR, "Additional privileges required."); 1928 priv_freeset(priv_set); 1929 return (-1); 1930 } 1931 priv_freeset(priv_set); 1932 return (0); 1933 } 1934 1935 static int 1936 ndmp_set_eprivs_all(void) 1937 { 1938 priv_set_t *priv_set; 1939 1940 if ((priv_set = priv_str_to_set("all", ",", NULL)) == NULL) { 1941 NDMP_LOG(LOG_ERR, "Could not set privileges to 'all'."); 1942 return (-1); 1943 } 1944 if (setppriv(PRIV_SET, PRIV_EFFECTIVE, priv_set) != 0) { 1945 NDMP_LOG(LOG_ERR, "Additional privileges required."); 1946 return (-1); 1947 } 1948 priv_freeset(priv_set); 1949 return (0); 1950 } 1951 1952 /* 1953 * Set the standard attributes of the file 1954 */ 1955 static void 1956 set_attr(char *name, tlm_acls_t *acls) 1957 { 1958 struct utimbuf tbuf; 1959 boolean_t priv_all = FALSE; 1960 struct stat64 *st; 1961 uid_t uid; 1962 gid_t gid; 1963 struct passwd *pwd; 1964 struct group *grp; 1965 1966 1967 if (!name || !acls) 1968 return; 1969 1970 st = &acls->acl_attr; 1971 NDMP_LOG(LOG_DEBUG, "set_attr: %s uid %d gid %d uname %s gname %s " 1972 "mode %o", name, st->st_uid, st->st_gid, acls->uname, acls->gname, 1973 st->st_mode); 1974 1975 uid = st->st_uid; 1976 if ((pwd = getpwnam(acls->uname)) != NULL) { 1977 NDMP_LOG(LOG_DEBUG, "set_attr: new uid %d old %d", 1978 pwd->pw_uid, uid); 1979 uid = pwd->pw_uid; 1980 } 1981 1982 gid = st->st_gid; 1983 if ((grp = getgrnam(acls->gname)) != NULL) { 1984 NDMP_LOG(LOG_DEBUG, "set_attr: new gid %d old %d", 1985 grp->gr_gid, gid); 1986 gid = grp->gr_gid; 1987 } 1988 1989 if (lchown(name, uid, gid)) 1990 NDMP_LOG(LOG_ERR, 1991 "Could not set uid or/and gid for file %s.", name); 1992 1993 if ((st->st_mode & (S_ISUID | S_ISGID)) != 0) { 1994 /* 1995 * Change effective privileges to 'all' which is required to 1996 * change setuid bit for 'root' owned files. If fails, just 1997 * send error to log file and proceed. 1998 */ 1999 if (ndmp_set_eprivs_all()) { 2000 NDMP_LOG(LOG_ERR, 2001 "Could not set effective privileges to 'all'."); 2002 } else { 2003 priv_all = TRUE; 2004 } 2005 } 2006 2007 if (chmod(name, st->st_mode)) 2008 NDMP_LOG(LOG_ERR, 2009 "Could not set correct file permission for file %s.", name); 2010 2011 if (priv_all == TRUE) { 2012 /* 2013 * Give up the 'all' privileges for effective sets and go back 2014 * to least required privileges. If fails, just send error to 2015 * log file and proceed. 2016 */ 2017 if (ndmp_set_eprivs_least()) 2018 NDMP_LOG(LOG_ERR, 2019 "Could not set least required privileges."); 2020 } 2021 2022 tbuf.modtime = st->st_mtime; 2023 tbuf.actime = st->st_atime; 2024 (void) utime(name, &tbuf); 2025 } 2026 2027 /* 2028 * Set the ACL info for the file 2029 */ 2030 static void 2031 set_acl(char *name, tlm_acls_t *acls) 2032 { 2033 int erc; 2034 acl_t *aclp = NULL; 2035 2036 if (name) 2037 NDMP_LOG(LOG_DEBUG, "set_acl: %s", name); 2038 if (acls != 0) { 2039 /* Need a place to save real modification time */ 2040 2041 set_attr(name, acls); 2042 2043 if (!acls->acl_non_trivial) { 2044 (void) memset(acls, 0, sizeof (tlm_acls_t)); 2045 NDMP_LOG(LOG_DEBUG, "set_acl: skipping trivial"); 2046 return; 2047 } 2048 2049 erc = acl_fromtext(acls->acl_info.attr_info, &aclp); 2050 if (erc != 0) { 2051 NDMP_LOG(LOG_DEBUG, 2052 "TAPE RESTORE> acl_fromtext errno %d", erc); 2053 } 2054 if (aclp) { 2055 erc = acl_set(name, aclp); 2056 if (erc < 0) { 2057 NDMP_LOG(LOG_DEBUG, 2058 "TAPE RESTORE> acl_set errno %d", errno); 2059 } 2060 acl_free(aclp); 2061 } 2062 (void) memset(acls, 0, sizeof (tlm_acls_t)); 2063 } 2064 } 2065 2066 /* 2067 * a wrapper to tlm_get_read_buffer so that 2068 * we can cleanly detect ABORT commands 2069 * without involving the TLM library with 2070 * our problems. 2071 */ 2072 static char * 2073 get_read_buffer(int want, 2074 int *error, 2075 int *actual_size, 2076 tlm_cmd_t *local_commands) 2077 { 2078 while (local_commands->tc_writer == TLM_RESTORE_RUN) { 2079 char *rec; 2080 rec = tlm_get_read_buffer(want, error, 2081 local_commands->tc_buffers, actual_size); 2082 if (rec != 0) { 2083 return (rec); 2084 } 2085 } 2086 2087 /* 2088 * the job is ending, give Writer a buffer that will never be read ... 2089 * it does not matter anyhow, we are aborting. 2090 */ 2091 *actual_size = RECORDSIZE; 2092 return (NULL); 2093 } 2094 2095 /* 2096 * Enable wildcard for restore options 2097 */ 2098 static boolean_t 2099 wildcard_enabled(void) 2100 { 2101 char *cp; 2102 2103 cp = ndmpd_get_prop_default(NDMP_RESTORE_WILDCARD_ENABLE, "n"); 2104 return ((toupper(*cp) == 'Y') ? TRUE : FALSE); 2105 } 2106 2107 2108 /* 2109 * Concatenate two names 2110 */ 2111 /*ARGSUSED*/ 2112 static char * 2113 catnames(struct rs_name_maker *rnp, char *buf, int pos, char *path) 2114 { 2115 char *rv; 2116 2117 rv = NULL; 2118 if (!buf) { 2119 NDMP_LOG(LOG_DEBUG, "buf is NULL"); 2120 } else if (!path) { 2121 NDMP_LOG(LOG_DEBUG, "path is NULL"); 2122 } else if (!rnp->rn_nlp) { 2123 NDMP_LOG(LOG_DEBUG, "rn_nlp is NULL [%s]", path); 2124 } else if (!tlm_cat_path(buf, rnp->rn_nlp, path)) { 2125 NDMP_LOG(LOG_DEBUG, "Path too long [%s][%s]", 2126 rnp->rn_nlp, path); 2127 } else 2128 rv = buf; 2129 2130 return (rv); 2131 } 2132 2133 2134 /* 2135 * Create a new name path for restore 2136 */ 2137 static char * 2138 rs_new_name(struct rs_name_maker *rnp, char *buf, int pos, char *path) 2139 { 2140 if (!rnp || !rnp->rn_fp) 2141 return (NULL); 2142 2143 return (*rnp->rn_fp)(rnp, buf, pos, path); 2144 } 2145 2146 /* 2147 * Iterate over ZFS metadata stored in the backup stream and use the callback 2148 * to restore it. 2149 */ 2150 int 2151 ndmp_iter_zfs(ndmp_context_t *nctx, int (*np_restore_property)(nvlist_t *, 2152 void *), void *ptr) 2153 { 2154 tlm_commands_t *cmds; 2155 ndmp_metadata_header_t *mhp; 2156 ndmp_metadata_property_t *mpp; 2157 tlm_cmd_t *lcmd; 2158 int actual_size; 2159 nvlist_t *nvl; 2160 nvlist_t *nvl_head; 2161 nvlist_t *valp; 2162 nvpair_t *nvp = NULL; 2163 nvpair_t *nvph = NULL; 2164 char plname[100]; 2165 char *mhbuf, *pp, *tp; 2166 int rv, i; 2167 int size, lsize, sz; 2168 int align = RECORDSIZE - 1; 2169 2170 if (nctx == NULL || (cmds = (tlm_commands_t *)nctx->nc_cmds) == NULL) 2171 return (-1); 2172 2173 nctx->nc_plname = plname; 2174 if ((lcmd = cmds->tcs_command) == NULL || 2175 lcmd->tc_buffers == NULL) 2176 return (-1); 2177 2178 size = sizeof (ndmp_metadata_header_t) + 2179 ZFS_MAX_PROPS * sizeof (ndmp_metadata_property_t); 2180 size += align; 2181 size &= ~align; 2182 2183 /* For nvlist cleanup */ 2184 if (nvlist_alloc(&nvl_head, NV_UNIQUE_NAME, 0) != 0) 2185 return (-1); 2186 2187 if ((mhbuf = malloc(size)) == NULL) 2188 return (-1); 2189 2190 /* LINTED improper alignment */ 2191 while ((mhp = (ndmp_metadata_header_t *)get_read_buffer(size, &rv, 2192 &actual_size, lcmd)) != NULL) { 2193 pp = mhbuf; 2194 2195 if (strncmp(mhp->nh_magic, ZFS_META_MAGIC, 2196 sizeof (mhp->nh_magic)) != 0) { 2197 /* No more metadata */ 2198 tlm_unget_read_buffer(lcmd->tc_buffers, actual_size); 2199 nvlist_free(nvl_head); 2200 return (0); 2201 } 2202 2203 (void) memcpy(pp, (char *)mhp, (actual_size < size) ? 2204 actual_size : size); 2205 pp += (actual_size < size) ? actual_size : size; 2206 2207 sz = actual_size; 2208 while (sz < size && 2209 ((tp = get_read_buffer(size - sz, &rv, &lsize, 2210 lcmd))) != NULL) { 2211 (void) memcpy(pp, tp, size - sz); 2212 sz += lsize; 2213 pp += lsize; 2214 } 2215 if (sz > size) { 2216 tlm_unget_read_buffer(lcmd->tc_buffers, sz - size); 2217 } 2218 /* LINTED improper alignment */ 2219 mhp = (ndmp_metadata_header_t *)mhbuf; 2220 2221 nctx->nc_plversion = mhp->nh_plversion; 2222 (void) strlcpy(plname, mhp->nh_plname, sizeof (plname)); 2223 2224 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 2225 goto nvlist_err; 2226 2227 mpp = &mhp->nh_property[0]; 2228 for (i = 0; i < mhp->nh_count && mpp; i++) { 2229 if (nvlist_alloc(&valp, NV_UNIQUE_NAME, 0) != 0 || 2230 nvlist_add_string(valp, "value", 2231 mpp->mp_value) != 0 || 2232 nvlist_add_string(valp, "source", 2233 mpp->mp_source) != 0 || 2234 nvlist_add_nvlist(nvl, mpp->mp_name, valp) != 0) 2235 goto nvlist_err; 2236 mpp++; 2237 } 2238 2239 if (np_restore_property(nvl, ptr) != 0) 2240 goto nvlist_err; 2241 2242 (void) nvlist_add_nvlist(nvl_head, "_", nvl); 2243 } 2244 free(mhbuf); 2245 2246 nvlist_free(nvl_head); 2247 return (0); 2248 2249 nvlist_err: 2250 free(mhbuf); 2251 while ((nvph = nvlist_next_nvpair(nvl_head, nvph)) != NULL && 2252 nvpair_value_nvlist(nvph, &nvl) == 0) { 2253 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL && 2254 nvpair_value_nvlist(nvp, &valp) == 0) { 2255 nvlist_free(valp); 2256 } 2257 nvlist_free(nvl); 2258 } 2259 nvlist_free(nvl_head); 2260 return (-1); 2261 } 2262 2263 /* 2264 * Returns the version number of the plugin which created the metadata 2265 */ 2266 uint_t 2267 ndmp_context_get_version(ndmp_context_t *nctx) 2268 { 2269 tlm_commands_t *cmds; 2270 ndmp_metadata_header_t *mhp; 2271 tlm_cmd_t *lcmd; 2272 int actual_size; 2273 int rv; 2274 int size; 2275 int align = RECORDSIZE - 1; 2276 2277 if (nctx == NULL || (cmds = (tlm_commands_t *)nctx->nc_cmds) == NULL) 2278 return (0); 2279 2280 if ((lcmd = cmds->tcs_command) == NULL || 2281 lcmd->tc_buffers == NULL) 2282 return (0); 2283 2284 size = sizeof (ndmp_metadata_header_t); 2285 size += align; 2286 size &= ~align; 2287 2288 /* LINTED improper alignment */ 2289 if ((mhp = (ndmp_metadata_header_t *)get_read_buffer(size, &rv, 2290 &actual_size, lcmd)) != NULL) { 2291 if (strncmp(mhp->nh_magic, ZFS_META_MAGIC, 2292 sizeof (mhp->nh_magic)) != 0) { 2293 /* No more metadata */ 2294 tlm_unget_read_buffer(lcmd->tc_buffers, actual_size); 2295 return (0); 2296 } 2297 2298 nctx->nc_plversion = mhp->nh_plversion; 2299 tlm_unget_read_buffer(lcmd->tc_buffers, actual_size); 2300 } 2301 2302 return (nctx->nc_plversion); 2303 } 2304