1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
|
/*
* Copyright (c) 2000-2001 Silicon Graphics, Inc.
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Directory attributes are written on tape during the directory dump phase,
* which precedes the non-dir dump phase. The directory attributes cannot be
* restored, however, until all of the non-dir files have been restored
* (directory timestamps would be wrong, directory inherit flags would
* interfere, etc.) These routines allow the directory attributes to be stored
* on disk when they are read from the dump stream so that they can be applied
* at a later time.
*/
#include <xfs/xfs.h>
#include <xfs/jdm.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <assert.h>
#include <string.h>
#include "config.h"
#include "types.h"
#include "util.h"
#include "mlog.h"
#include "global.h"
#include "drive.h"
#include "media.h"
#include "content.h"
#include "content_inode.h"
#include "dirattr.h"
#include "openutil.h"
#include "mmap.h"
/* Create a file, try to reserve space for it, and return the fd. */
int
create_filled_file(
const char *pathname,
off64_t size)
{
struct flock64 fl = {
.l_len = size,
};
int fd;
int ret;
ret = unlink(pathname);
if (ret && errno != ENOENT)
return ret;
fd = open(pathname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (fd < 0)
return fd;
#ifdef HAVE_FALLOCATE
ret = fallocate(fd, 0, 0, size);
if (ret && (errno != EOPNOTSUPP && errno != ENOTTY))
mlog(MLOG_VERBOSE | MLOG_NOTE,
_("attempt to reserve %lld bytes for %s using %s failed: %s (%d)\n"),
size, pathname, "fallocate",
strerror(errno), errno);
if (ret == 0)
return fd;
#endif
ret = ioctl(fd, XFS_IOC_RESVSP64, &fl);
if (ret && (errno != EOPNOTSUPP && errno != ENOTTY))
mlog(MLOG_VERBOSE | MLOG_NOTE,
_("attempt to reserve %lld bytes for %s using %s failed: %s (%d)\n"),
size, pathname, "XFS_IOC_RESVSP64",
strerror(errno), errno);
return fd;
}
/* structure definitions used locally ****************************************/
/* node handle limits
*/
#ifdef DIRATTRCHK
/* macros for manipulating dirattr handles when handle consistency
* checking is enabled. the upper bits of a handle will be loaded
* with a handle checksum.
*/
#define HDLSUMCNT 4
#define HDLSUMSHIFT (NBBY * sizeof (dah_t) - HDLSUMCNT)
#define HDLSUMLOMASK ((1 << HDLSUMCNT) - 1)
#define HDLSUMMASK (HDLSUMLOMASK << HDLSUMSHIFT)
#define HDLDIXCNT HDLSUMSHIFT
#define HDLDIXMASK ((1 << HDLDIXCNT) - 1)
#define HDLGETSUM(h) ((uint16_t) \
(((int)h >> HDLSUMSHIFT) \
& \
HDLSUMLOMASK))
#define HDLGETDIX(h) ((dix_t)((int)h & HDLDIXMASK))
#define HDLMKHDL(s, d) ((dah_t)((((int)s << HDLSUMSHIFT)\
& \
HDLSUMMASK) \
| \
((int)d & HDLDIXMASK)))
#define DIX_MAX ((off64_t)HDLDIXMASK)
/* each dirattr will hold two check fields: a handle checksum, and unique
* pattern, to differentiate a valid dirattr from random file contents.
*/
#define DIRATTRUNQ 0xa116
#else /* DIRATTRCHK */
#define DIX_MAX (((off64_t)1 \
<< \
((off64_t)NBBY \
* \
(off64_t)sizeof(dah_t))) \
- \
(off64_t)2) /* 2 to avoid DAH_NULL */
#endif /* DIRATTRCHK */
/* dirattr definition
*/
struct dirattr {
#ifdef DIRATTRCHK
uint16_t d_unq;
uint16_t d_sum;
#endif /* DIRATTRCHK */
mode_t d_mode;
uid_t d_uid;
gid_t d_gid;
time32_t d_atime;
time32_t d_mtime;
time32_t d_ctime;
uint32_t d_xflags;
uint32_t d_extsize;
uint32_t d_projid;
uint32_t d_dmevmask;
uint32_t d_dmstate;
off64_t d_extattroff;
};
typedef struct dirattr dirattr_t;
#define DIRATTR_EXTATTROFFNULL ((off64_t)OFF64MAX)
/* dirattr persistent context definition
*/
struct dirattr_pers {
off64_t dp_appendoff;
};
typedef struct dirattr_pers dirattr_pers_t;
#define DIRATTR_PERS_SZ pgsz
/* dirattr transient context definition
*/
#define DIRATTR_BUFSIZE 32768
struct dirattr_tran {
char *dt_pathname;
int dt_fd;
bool_t dt_at_endpr;
dah_t dt_cachedh;
dirattr_t dt_cached_dirattr;
size_t dt_off;
char dt_buf[DIRATTR_BUFSIZE];
char *dt_extattrpathname;
int dt_extattrfd;
bool_t dt_extattrfdbadpr;
};
typedef struct dirattr_tran dirattr_tran_t;
/* a dirattr is identified internally by its index into the backing store.
* this index is the offset of the dirattr (relative to the end of the dirattr
* persistent state hdr) into the backing store divided by the size of a
* dirattr. a special index is reserved to represent the null index. a type
* is defined for dirattr index (dix_t). it is a 64 bit signed for direct use
* in the lseek64 system call.
*/
typedef off64_t dix_t;
#define DIX2OFF(dix) ((off64_t)(dix * (off64_t)sizeof(dirattr_t) \
+ \
(off64_t)DIRATTR_PERS_SZ))
#define OFF2DIX(doff) ((dix_t)((doff - (off64_t)DIRATTR_PERS_SZ) \
/ \
(off64_t)sizeof(dirattr_t)))
/* declarations of externally defined global symbols *************************/
extern size_t pgsz;
/* forward declarations of locally defined static functions ******************/
static void dirattr_get(dah_t);
static void dirattr_cacheflush(void);
#ifdef DIRATTRCHK
static uint16_t calcdixcum(dix_t dix);
#endif /* DIRATTRCHK */
/* definition of locally defined global variables ****************************/
/* definition of locally defined static variables *****************************/
static char *dirattrfile = "dirattr";
static char *dirextattrfile = "dirextattr";
static dirattr_tran_t *dtp = 0;
static dirattr_pers_t *dpp = 0;
/* definition of locally defined global functions ****************************/
bool_t
dirattr_init(char *hkdir, bool_t resume, uint64_t dircnt)
{
if (dtp) {
return BOOL_TRUE;
}
/* sanity checks
*/
assert(sizeof(dirattr_pers_t) <= DIRATTR_PERS_SZ);
assert(!dtp);
assert(!dpp);
/* allocate and initialize context
*/
dtp = (dirattr_tran_t *)calloc(1, sizeof(dirattr_tran_t));
assert(dtp);
dtp->dt_cachedh = DAH_NULL;
dtp->dt_fd = -1;
dtp->dt_extattrfd = -1;
/* generate a string containing the pathname of the dirattr file
*/
dtp->dt_pathname = open_pathalloc(hkdir, dirattrfile, 0);
/* open the dirattr file
*/
if (resume) {
/* open existing file
*/
dtp->dt_fd = open(dtp->dt_pathname, O_RDWR);
if (dtp->dt_fd < 0) {
mlog(MLOG_NORMAL | MLOG_ERROR, _(
"could not find directory attributes file %s: "
"%s\n"),
dtp->dt_pathname,
strerror(errno));
return BOOL_FALSE;
}
} else {
dtp->dt_fd = create_filled_file(dtp->dt_pathname,
DIRATTR_PERS_SZ + (dircnt * sizeof(struct dirattr)));
if (dtp->dt_fd < 0) {
mlog(MLOG_NORMAL | MLOG_ERROR, _(
"could not create directory attributes file %s: "
"%s\n"),
dtp->dt_pathname,
strerror(errno));
return BOOL_FALSE;
}
}
/* mmap the persistent descriptor
*/
assert(!(DIRATTR_PERS_SZ % pgsz));
dpp = (dirattr_pers_t *)mmap_autogrow(DIRATTR_PERS_SZ,
dtp->dt_fd,
(off_t)0);
assert(dpp);
if (dpp == (dirattr_pers_t *)-1) {
mlog(MLOG_NORMAL | MLOG_ERROR, _(
"unable to map %s: %s\n"),
dtp->dt_pathname,
strerror(errno));
return BOOL_FALSE;
}
/* initialize persistent state
*/
if (!resume) {
dpp->dp_appendoff = (off64_t)DIRATTR_PERS_SZ;
}
/* initialize transient state
*/
dtp->dt_at_endpr = BOOL_FALSE;
/* calculate the dir extattr pathname, and set the fd to -1.
* file will be created on demand.
*/
dtp->dt_extattrpathname = open_pathalloc(hkdir, dirextattrfile, 0);
dtp->dt_extattrfd = -1;
dtp->dt_extattrfdbadpr = BOOL_FALSE;
if (resume) {
(void)unlink(dtp->dt_extattrpathname);
}
return BOOL_TRUE;
}
void
dirattr_cleanup(void)
{
/* REFERENCED */
int rval;
if (!dtp) {
return;
}
if (dpp) {
rval = munmap((void *)dpp, DIRATTR_PERS_SZ);
assert(!rval);
dpp = 0;
}
if (dtp->dt_fd >= 0) {
(void)close(dtp->dt_fd);
dtp->dt_fd = -1;
}
if (dtp->dt_pathname) {
(void)unlink(dtp->dt_pathname);
free((void *)dtp->dt_pathname);
}
if (dtp->dt_extattrfd >= 0) {
(void)close(dtp->dt_extattrfd);
dtp->dt_extattrfd = -1;
}
if (dtp->dt_extattrpathname) {
(void)unlink(dtp->dt_extattrpathname);
free((void *)dtp->dt_extattrpathname);
}
free((void *)dtp);
dtp = 0;
}
dah_t
dirattr_add(filehdr_t *fhdrp)
{
dirattr_t dirattr;
off64_t oldoff;
dix_t dix;
#ifdef DIRATTRCHK
uint16_t sum;
#endif /* DIRATTRCHK */
dah_t dah;
/* sanity checks
*/
assert(dtp);
assert(dpp);
/* make sure file pointer is positioned to write at end of file
*/
if (!dtp->dt_at_endpr) {
off64_t newoff;
newoff = lseek64(dtp->dt_fd, dpp->dp_appendoff, SEEK_SET);
if (newoff == (off64_t)-1) {
mlog(MLOG_NORMAL | MLOG_ERROR, _(
"lseek of dirattr failed: %s\n"),
strerror(errno));
return DAH_NULL;
}
assert(dpp->dp_appendoff == newoff);
dtp->dt_at_endpr = BOOL_TRUE;
}
if (dtp->dt_off + sizeof(dirattr_t) > sizeof(dtp->dt_buf)) {
if (dirattr_flush() != RV_OK) {
return DAH_NULL;
}
}
/* calculate the index of this dirattr
*/
oldoff = dpp->dp_appendoff;
dix = OFF2DIX(oldoff);
assert(dix <= DIX_MAX);
/* populate a dirattr
*/
dirattr.d_mode = (mode_t)fhdrp->fh_stat.bs_mode;
dirattr.d_uid = (uid_t)fhdrp->fh_stat.bs_uid;
dirattr.d_gid = (gid_t)fhdrp->fh_stat.bs_gid;
dirattr.d_atime = (time32_t)fhdrp->fh_stat.bs_atime.tv_sec;
dirattr.d_mtime = (time32_t)fhdrp->fh_stat.bs_mtime.tv_sec;
dirattr.d_ctime = (time32_t)fhdrp->fh_stat.bs_ctime.tv_sec;
dirattr.d_xflags = fhdrp->fh_stat.bs_xflags;
dirattr.d_extsize = (uint32_t)fhdrp->fh_stat.bs_extsize;
dirattr.d_projid = bstat_projid(&(fhdrp->fh_stat));
dirattr.d_dmevmask = fhdrp->fh_stat.bs_dmevmask;
dirattr.d_dmstate = (uint32_t)fhdrp->fh_stat.bs_dmstate;
#ifdef DIRATTRCHK
dirattr.d_unq = DIRATTRUNQ;
sum = calcdixcum(dix);
dirattr.d_sum = sum;
#endif /* DIRATTRCHK */
dirattr.d_extattroff = DIRATTR_EXTATTROFFNULL;
/* write the entry into our buffer
*/
memcpy(dtp->dt_buf + dtp->dt_off, (void *)&dirattr, sizeof(dirattr_t));
dtp->dt_off += sizeof(dirattr_t);
/* update the next write offset
*/
assert(dpp->dp_appendoff <= OFF64MAX - (off64_t)sizeof(dirattr_t));
dpp->dp_appendoff += (off64_t)sizeof(dirattr_t);
#ifdef DIRATTRCHK
dah = HDLMKHDL(sum, dix);
#else /* DIRATTRCHK */
dah = (dah_t)dix;
#endif /* DIRATTRCHK */
return dah;
}
void
dirattr_addextattr(dah_t dah, extattrhdr_t *ahdrp)
{
off64_t oldoff;
off64_t off;
off64_t seekoff;
off64_t nulloff;
int nread;
int nwritten;
/* pull the selected dir attributes into the cache
*/
dirattr_get(dah);
/* open/create extended attributes file if not yet done
*/
if (dtp->dt_extattrfd < 0) {
if (dtp->dt_extattrfdbadpr) {
return;
}
dtp->dt_extattrfd = open(dtp->dt_extattrpathname,
O_RDWR | O_CREAT,
S_IRUSR | S_IWUSR);
if (dtp->dt_extattrfd < 0) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"could not open/create directory "
"extended attributes file %s: "
"%s (%d)\n"),
dtp->dt_extattrpathname,
strerror(errno),
errno);
dtp->dt_extattrfdbadpr = BOOL_TRUE;
return;
}
}
/* seek to the end of the dir extattr list
*/
off = dtp->dt_cached_dirattr.d_extattroff;
oldoff = DIRATTR_EXTATTROFFNULL;
while (off != DIRATTR_EXTATTROFFNULL) {
seekoff = lseek64(dtp->dt_extattrfd, off, SEEK_SET);
if (seekoff < 0) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"could not seek to into extended attributes "
"file %s: "
"%s (%d)\n"),
dtp->dt_extattrpathname,
strerror(errno),
errno);
dtp->dt_extattrfdbadpr = BOOL_TRUE;
return;
}
assert(seekoff == off);
oldoff = off;
nread = read(dtp->dt_extattrfd,
(void *)&off,
sizeof(off));
if (nread != (int)sizeof(off)) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"could not read extended attributes "
"file %s: "
"%s (%d)\n"),
dtp->dt_extattrpathname,
strerror(errno),
errno);
dtp->dt_extattrfdbadpr = BOOL_TRUE;
return;
}
}
/* append the extended attributes
*/
off = lseek64(dtp->dt_extattrfd, 0, SEEK_END);
if (off < 0) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"could not seek to end of extended attributes "
"file %s: "
"%s (%d)\n"),
dtp->dt_extattrpathname,
strerror(errno),
errno);
dtp->dt_extattrfdbadpr = BOOL_TRUE;
return;
}
nulloff = DIRATTR_EXTATTROFFNULL;
nwritten = write(dtp->dt_extattrfd,
(void *)&nulloff,
sizeof(nulloff));
if (nwritten != (int)sizeof(nulloff)) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"could not write extended attributes "
"file %s: "
"%s (%d)\n"),
dtp->dt_extattrpathname,
strerror(errno),
errno);
dtp->dt_extattrfdbadpr = BOOL_TRUE;
return;
}
nwritten = write(dtp->dt_extattrfd, (void *)ahdrp, ahdrp->ah_sz);
if (nwritten != (int)(ahdrp->ah_sz)) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"could not write at end of extended attributes "
"file %s: "
"%s (%d)\n"),
dtp->dt_extattrpathname,
strerror(errno),
errno);
dtp->dt_extattrfdbadpr = BOOL_TRUE;
return;
}
/* fill in the offset of the extended attributes into the
* linked list
*/
if (oldoff == DIRATTR_EXTATTROFFNULL) {
dtp->dt_cached_dirattr.d_extattroff = off;
dirattr_cacheflush();
} else {
seekoff = lseek64(dtp->dt_extattrfd, oldoff, SEEK_SET);
if (seekoff < 0) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"could not seek to into extended attributes "
"file %s: "
"%s (%d)\n"),
dtp->dt_extattrpathname,
strerror(errno),
errno);
dtp->dt_extattrfdbadpr = BOOL_TRUE;
return;
}
assert(seekoff == oldoff);
nwritten = write(dtp->dt_extattrfd,
(void *)&off,
sizeof(off));
if (nwritten != (int)sizeof(off)) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"could not write extended attributes "
"file %s: "
"%s (%d)\n"),
dtp->dt_extattrpathname,
strerror(errno),
errno);
dtp->dt_extattrfdbadpr = BOOL_TRUE;
return;
}
}
}
bool_t
dirattr_cb_extattr(dah_t dah,
bool_t (*cbfunc)(extattrhdr_t *ahdrp,
void *ctxp),
extattrhdr_t *ahdrp,
void *ctxp)
{
off64_t off;
/* pull the selected dir attributes into the cache
*/
dirattr_get(dah);
/* open/create extended attributes file if not yet done
*/
if (dtp->dt_extattrfd < 0) {
if (dtp->dt_extattrfdbadpr) {
return BOOL_TRUE;
}
dtp->dt_extattrfd = open(dtp->dt_extattrpathname,
O_RDWR | O_CREAT,
S_IRUSR | S_IWUSR);
if (dtp->dt_extattrfd < 0) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"could not open/create directory "
"extended attributes file %s: "
"%s (%d)\n"),
dtp->dt_extattrpathname,
strerror(errno),
errno);
dtp->dt_extattrfdbadpr = BOOL_TRUE;
return BOOL_TRUE;
}
}
/* walk through the dirattr list for this dah
*/
off = dtp->dt_cached_dirattr.d_extattroff;
while (off != DIRATTR_EXTATTROFFNULL) {
off64_t seekoff;
int nread;
off64_t nextoff;
size_t recsz;
bool_t ok;
/* seek to the extattr
*/
seekoff = lseek64(dtp->dt_extattrfd, off, SEEK_SET);
if (seekoff < 0) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"could not seek to into extended attributes "
"file %s: "
"%s (%d)\n"),
dtp->dt_extattrpathname,
strerror(errno),
errno);
dtp->dt_extattrfdbadpr = BOOL_TRUE;
return BOOL_TRUE;
}
assert(seekoff == off);
/* peel off the next offset
*/
nread = read(dtp->dt_extattrfd,
(void *)&nextoff,
sizeof(nextoff));
if (nread != (int)sizeof(nextoff)) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"could not read extended attributes "
"file %s: "
"%s (%d)\n"),
dtp->dt_extattrpathname,
strerror(errno),
errno);
dtp->dt_extattrfdbadpr = BOOL_TRUE;
return BOOL_TRUE;
}
/* read the extattr hdr
*/
nread = read(dtp->dt_extattrfd,
(void *)ahdrp,
EXTATTRHDR_SZ);
if (nread != EXTATTRHDR_SZ) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"could not read extended attributes "
"file %s: "
"%s (%d)\n"),
dtp->dt_extattrpathname,
strerror(errno),
errno);
dtp->dt_extattrfdbadpr = BOOL_TRUE;
return BOOL_TRUE;
}
/* read the remainder of the extattr
*/
recsz = (size_t)ahdrp->ah_sz;
assert(recsz >= EXTATTRHDR_SZ);
nread = read(dtp->dt_extattrfd,
(void *)&ahdrp[1],
recsz - EXTATTRHDR_SZ);
if (nread != (int)(recsz - EXTATTRHDR_SZ)) {
mlog(MLOG_NORMAL | MLOG_WARNING, _(
"could not read extended attributes "
"file %s: "
"%s (%d)\n"),
dtp->dt_extattrpathname,
strerror(errno),
errno);
dtp->dt_extattrfdbadpr = BOOL_TRUE;
return BOOL_TRUE;
}
/* call the callback func
*/
ok = (*cbfunc)(ahdrp, ctxp);
if (!ok) {
return BOOL_FALSE;
}
/* go th the next one
*/
off = nextoff;
}
return BOOL_TRUE;
}
void
dirattr_update(dah_t dah, filehdr_t *fhdrp)
{
dix_t dix;
#ifdef DIRATTRCHK
uint16_t sum;
#endif /* DIRATTRCHK */
off64_t argoff;
off64_t newoff;
dirattr_t dirattr;
int nwritten;
/* sanity checks
*/
assert(dtp);
assert(dpp);
assert(dah != DAH_NULL);
#ifdef DIRATTRCHK
sum = HDLGETSUM(dah);
dix = HDLGETDIX(dah);
#else /* DIRATTRCHK */
dix = (dix_t)dah;
#endif /* DIRATTRCHK */
assert(dix >= 0);
assert(dix <= DIX_MAX);
argoff = DIX2OFF(dix);
assert(argoff >= 0);
assert(argoff >= (off64_t)DIRATTR_PERS_SZ);
assert(argoff <= dpp->dp_appendoff - (off64_t)sizeof(dirattr_t));
#ifdef DIRATTRCHK
dirattr_get(dah);
assert(dtp->dt_cached_dirattr.d_unq == DIRATTRUNQ);
assert(dtp->dt_cached_dirattr.d_sum == sum);
#endif /* DIRATTRCHK */
if (dtp->dt_at_endpr && dtp->dt_off) {
if (dirattr_flush() != RV_OK) {
assert(0);
return;
}
}
/* seek to the dirattr
*/
newoff = lseek64(dtp->dt_fd, argoff, SEEK_SET);
if (newoff == (off64_t)-1) {
mlog(MLOG_NORMAL, _(
"lseek of dirattr failed: %s\n"),
strerror(errno));
assert(0);
}
assert(newoff == argoff);
/* populate a dirattr
*/
dirattr.d_mode = (mode_t)fhdrp->fh_stat.bs_mode;
dirattr.d_uid = (uid_t)fhdrp->fh_stat.bs_uid;
dirattr.d_gid = (gid_t)fhdrp->fh_stat.bs_gid;
dirattr.d_atime = (time32_t)fhdrp->fh_stat.bs_atime.tv_sec;
dirattr.d_mtime = (time32_t)fhdrp->fh_stat.bs_mtime.tv_sec;
dirattr.d_ctime = (time32_t)fhdrp->fh_stat.bs_ctime.tv_sec;
dirattr.d_xflags = fhdrp->fh_stat.bs_xflags;
dirattr.d_extsize = (uint32_t)fhdrp->fh_stat.bs_extsize;
dirattr.d_projid = bstat_projid(&(fhdrp->fh_stat));
dirattr.d_dmevmask = fhdrp->fh_stat.bs_dmevmask;
dirattr.d_dmstate = (uint32_t)fhdrp->fh_stat.bs_dmstate;
dirattr.d_extattroff = DIRATTR_EXTATTROFFNULL;
/* write the dirattr
*/
nwritten = write(dtp->dt_fd, (void *)&dirattr, sizeof(dirattr));
if ((size_t)nwritten != sizeof(dirattr)) {
mlog(MLOG_NORMAL, _(
"update of dirattr failed: %s\n"),
strerror(errno));
assert(0);
}
dtp->dt_at_endpr = BOOL_FALSE;
dtp->dt_cachedh = dah;
}
/* ARGSUSED */
void
dirattr_del(dah_t dah)
{
}
mode_t
dirattr_get_mode(dah_t dah)
{
dirattr_get(dah);
return dtp->dt_cached_dirattr.d_mode;
}
uid_t
dirattr_get_uid(dah_t dah)
{
dirattr_get(dah);
return dtp->dt_cached_dirattr.d_uid;
}
uid_t
dirattr_get_gid(dah_t dah)
{
dirattr_get(dah);
return dtp->dt_cached_dirattr.d_gid;
}
time32_t
dirattr_get_atime(dah_t dah)
{
dirattr_get(dah);
return dtp->dt_cached_dirattr.d_atime;
}
time32_t
dirattr_get_mtime(dah_t dah)
{
dirattr_get(dah);
return dtp->dt_cached_dirattr.d_mtime;
}
time32_t
dirattr_get_ctime(dah_t dah)
{
dirattr_get(dah);
return dtp->dt_cached_dirattr.d_ctime;
}
uint32_t
dirattr_get_xflags(dah_t dah)
{
dirattr_get(dah);
return dtp->dt_cached_dirattr.d_xflags;
}
uint32_t
dirattr_get_extsize(dah_t dah)
{
dirattr_get(dah);
return dtp->dt_cached_dirattr.d_extsize;
}
uint32_t
dirattr_get_projid(dah_t dah)
{
dirattr_get(dah);
return dtp->dt_cached_dirattr.d_projid;
}
uint32_t
dirattr_get_dmevmask(dah_t dah)
{
dirattr_get(dah);
return dtp->dt_cached_dirattr.d_dmevmask;
}
uint32_t
dirattr_get_dmstate(dah_t dah)
{
dirattr_get(dah);
return dtp->dt_cached_dirattr.d_dmstate;
}
rv_t
dirattr_flush()
{
ssize_t nwritten;
/* sanity checks
*/
assert (dtp);
if (dtp->dt_off) {
/* write the accumulated dirattr entries
*/
nwritten = write(dtp->dt_fd, (void *)dtp->dt_buf, dtp->dt_off);
if (nwritten != dtp->dt_off) {
if (nwritten < 0) {
mlog(MLOG_NORMAL | MLOG_ERROR,
_("write of dirattr buffer failed: %s\n"),
strerror(errno));
} else {
mlog(MLOG_NORMAL | MLOG_ERROR,
_("write of dirattr buffer failed: "
"expected to write %ld, actually "
"wrote %ld\n"), dtp->dt_off, nwritten);
}
assert(0);
return RV_UNKNOWN;
}
dtp->dt_off = 0;
}
return RV_OK;
}
/* definition of locally defined static functions ****************************/
static void
dirattr_get(dah_t dah)
{
dix_t dix;
off64_t argoff;
off64_t newoff;
int nread;
#ifdef DIRATTRCHK
uint16_t sum;
#endif /* DIRATTRCHK */
/* sanity checks
*/
assert(dtp);
assert(dpp);
assert(dah != DAH_NULL);
/* if we are already holding this dirattr in cache,
* just return
*/
if (dtp->dt_cachedh == dah) {
return;
}
#ifdef DIRATTRCHK
sum = HDLGETSUM(dah);
dix = HDLGETDIX(dah);
#else /* DIRATTRCHK */
dix = (dix_t)dah;
#endif /* DIRATTRCHK */
assert(dix >= 0);
assert(dix <= DIX_MAX);
argoff = DIX2OFF(dix);
assert(argoff >= 0);
assert(argoff >= (off64_t)DIRATTR_PERS_SZ);
assert(argoff <= dpp->dp_appendoff - (off64_t)sizeof(dirattr_t));
if (dtp->dt_at_endpr && dtp->dt_off) {
if (dirattr_flush() != RV_OK) {
assert(0);
return;
}
}
/* seek to the dirattr
*/
newoff = lseek64(dtp->dt_fd, argoff, SEEK_SET);
if (newoff == (off64_t)-1) {
mlog(MLOG_NORMAL, _(
"lseek of dirattr failed: %s\n"),
strerror(errno));
assert(0);
}
assert(newoff == argoff);
/* read the dirattr
*/
nread = read(dtp->dt_fd,
(void *)&dtp->dt_cached_dirattr,
sizeof(dtp->dt_cached_dirattr));
if ((size_t)nread != sizeof(dtp->dt_cached_dirattr)) {
mlog(MLOG_NORMAL, _(
"read of dirattr failed: %s\n"),
strerror(errno));
assert(0);
}
#ifdef DIRATTRCHK
assert(dtp->dt_cached_dirattr.d_unq == DIRATTRUNQ);
assert(dtp->dt_cached_dirattr.d_sum == sum);
#endif /* DIRATTRCHK */
dtp->dt_at_endpr = BOOL_FALSE;
dtp->dt_cachedh = dah;
}
static void
dirattr_cacheflush(void)
{
dah_t dah;
dix_t dix;
#ifdef DIRATTRCHK
uint16_t sum;
#endif /* DIRATTRCHK */
off64_t argoff;
off64_t newoff;
int nwritten;
/* sanity checks
*/
assert(dtp);
assert(dpp);
/* if nothing in the cache, ignore
*/
dah = dtp->dt_cachedh;
assert(dah != DAH_NULL);
if (dah == DAH_NULL) {
return;
}
#ifdef DIRATTRCHK
sum = HDLGETSUM(dah);
dix = HDLGETDIX(dah);
#else /* DIRATTRCHK */
dix = (dix_t)dah;
#endif /* DIRATTRCHK */
#ifdef DIRATTRCHK
assert(dtp->dt_cached_dirattr.d_unq == DIRATTRUNQ);
assert(dtp->dt_cached_dirattr.d_sum == sum);
#endif /* DIRATTRCHK */
assert(dix >= 0);
assert(dix <= DIX_MAX);
argoff = DIX2OFF(dix);
assert(argoff >= 0);
assert(argoff >= (off64_t)DIRATTR_PERS_SZ);
assert(argoff <= dpp->dp_appendoff - (off64_t)sizeof(dirattr_t));
/* seek to the dirattr
*/
newoff = lseek64(dtp->dt_fd, argoff, SEEK_SET);
if (newoff == (off64_t)-1) {
mlog(MLOG_NORMAL, _(
"lseek of dirattr failed: %s\n"),
strerror(errno));
assert(0);
}
assert(newoff == argoff);
/* write the dirattr
*/
nwritten = write(dtp->dt_fd,
(void *)&dtp->dt_cached_dirattr,
sizeof(dtp->dt_cached_dirattr));
if ((size_t)nwritten != sizeof(dtp->dt_cached_dirattr)) {
mlog(MLOG_NORMAL, _(
"flush of dirattr failed: %s\n"),
strerror(errno));
assert(0);
}
dtp->dt_at_endpr = BOOL_FALSE;
}
#ifdef DIRATTRCHK
static uint16_t
calcdixcum(dix_t dix)
{
uint16_t sum;
ix_t nibcnt;
ix_t nibix;
assert((sizeof(dah_t) / HDLSUMCNT) * HDLSUMCNT == sizeof(dah_t));
nibcnt = (sizeof(dah_t) / HDLSUMCNT) - 1;
sum = 0;
for (nibix = 0; nibix < nibcnt; nibix++) {
sum += (uint16_t)(dix & HDLSUMLOMASK);
dix >>= HDLSUMCNT;
}
sum = (uint16_t)((~sum + 1) & HDLSUMLOMASK);
return sum;
}
#endif /* DIRATTRCHK */
|