| File: | volser/vol-dump.c |
| Location: | line 832, column 2 |
| Description: | Value stored to 'code' is never read |
| 1 | /* |
| 2 | * Copyright 2000, International Business Machines Corporation and others. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This software has been released under the terms of the IBM Public |
| 6 | * License. For details, see the LICENSE file in the top-level source |
| 7 | * directory or online at http://www.openafs.org/dl/license10.html |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | System: VICE-TWO |
| 12 | Module: vol-dump.c |
| 13 | Institution: The Information Technology Center, Carnegie-Mellon University |
| 14 | |
| 15 | */ |
| 16 | |
| 17 | #include <afsconfig.h> |
| 18 | #include <afs/param.h> |
| 19 | |
| 20 | #ifdef IGNORE_SOME_GCC_WARNINGS |
| 21 | # pragma GCC diagnostic warning "-Wformat" |
| 22 | #endif |
| 23 | |
| 24 | #include <roken.h> |
| 25 | |
| 26 | #include <ctype.h> |
| 27 | |
| 28 | #include <afs/cmd.h> |
| 29 | #include <rx/xdr.h> |
| 30 | #include <afs/afsint.h> |
| 31 | #include <afs/nfs.h> |
| 32 | #include <afs/errors.h> |
| 33 | #include <lock.h> |
| 34 | #include <lwp.h> |
| 35 | #include <afs/afssyscalls.h> |
| 36 | #include <afs/ihandle.h> |
| 37 | #include <afs/vnode.h> |
| 38 | #include <afs/volume.h> |
| 39 | #include <afs/partition.h> |
| 40 | #include <afs/viceinode.h> |
| 41 | #include <afs/afssyscalls.h> |
| 42 | #include <afs/acl.h> |
| 43 | #include <afs/dir.h> |
| 44 | #include <afs/com_err.h> |
| 45 | |
| 46 | #include "volser.h" |
| 47 | #include "volint.h" |
| 48 | #include "dump.h" |
| 49 | |
| 50 | #define afs_putint32(p, v)*p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p ++ = v *p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p++ = v |
| 51 | #define afs_putshort(p, v)*p++ = v>>8, *p++ = v *p++ = v>>8, *p++ = v |
| 52 | |
| 53 | int VolumeChanged; /* needed by physio - leave alone */ |
| 54 | int verbose = 0; |
| 55 | |
| 56 | /* Forward Declarations */ |
| 57 | void HandleVolume(struct DiskPartition64 *partP, char *name, char *filename, int fromtime); |
| 58 | Volume *AttachVolume(struct DiskPartition64 *dp, char *volname, |
| 59 | struct VolumeHeader *header); |
| 60 | static void DoMyVolDump(Volume * vp, struct DiskPartition64 *dp, |
| 61 | char *dumpfile, int fromtime); |
| 62 | |
| 63 | #ifndef AFS_NT40_ENV |
| 64 | #include "AFS_component_version_number.c" |
| 65 | #endif |
| 66 | |
| 67 | char name[VMAXPATHLEN64]; |
| 68 | |
| 69 | |
| 70 | int |
| 71 | ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version) |
| 72 | { |
| 73 | int code; |
| 74 | |
| 75 | code = IH_IREAD(ih, 0, to, size)namei_iread(ih, 0, to, size); |
| 76 | if (code != size) |
| 77 | return -1; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | Volume * |
| 84 | AttachVolume(struct DiskPartition64 * dp, char *volname, |
| 85 | struct VolumeHeader * header) |
| 86 | { |
| 87 | Volume *vp; |
| 88 | afs_int32 ec = 0; |
| 89 | |
| 90 | vp = (Volume *) calloc(1, sizeof(Volume)); |
| 91 | vp->specialStatus = 0; |
| 92 | vp->device = dp->device; |
| 93 | vp->partition = dp; |
| 94 | IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,((vp->vnodeIndex[0].handle) = ih_init((dp->device), (header ->parent), (header->largeVnodeIndex))) |
| 95 | header->largeVnodeIndex)((vp->vnodeIndex[0].handle) = ih_init((dp->device), (header ->parent), (header->largeVnodeIndex))); |
| 96 | IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,((vp->vnodeIndex[1].handle) = ih_init((dp->device), (header ->parent), (header->smallVnodeIndex))) |
| 97 | header->smallVnodeIndex)((vp->vnodeIndex[1].handle) = ih_init((dp->device), (header ->parent), (header->smallVnodeIndex))); |
| 98 | IH_INIT(vp->diskDataHandle, dp->device, header->parent,((vp->diskDataHandle) = ih_init((dp->device), (header-> parent), (header->volumeInfo))) |
| 99 | header->volumeInfo)((vp->diskDataHandle) = ih_init((dp->device), (header-> parent), (header->volumeInfo))); |
| 100 | IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable)((((vp)->linkHandle)) = ih_init((dp->device), (header-> parent), (header->linkTable))); |
| 101 | vp->cacheCheck = 0; /* XXXX */ |
| 102 | vp->shuttingDown = 0; |
| 103 | vp->goingOffline = 0; |
| 104 | vp->nUsers = 1; |
| 105 | vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header)); |
| 106 | ec = ReadHdr1(V_diskDataHandle(vp)((vp)->diskDataHandle), (char *)&V_disk(vp)((vp)->header->diskstuff), |
| 107 | sizeof(V_disk(vp)((vp)->header->diskstuff)), VOLUMEINFOMAGIC((bit32)0x78a1b2c5), VOLUMEINFOVERSION1); |
| 108 | if (!ec) { |
| 109 | struct IndexFileHeader iHead; |
| 110 | ec = ReadHdr1(vp->vnodeIndex[vSmall1].handle, (char *)&iHead, |
| 111 | sizeof(iHead), SMALLINDEXMAGIC0x99776655, SMALLINDEXVERSION1); |
| 112 | } |
| 113 | if (!ec) { |
| 114 | struct IndexFileHeader iHead; |
| 115 | ec = ReadHdr1(vp->vnodeIndex[vLarge0].handle, (char *)&iHead, |
| 116 | sizeof(iHead), LARGEINDEXMAGIC0x88664433, LARGEINDEXVERSION1); |
| 117 | } |
| 118 | #ifdef AFS_NAMEI_ENV1 |
| 119 | if (!ec) { |
| 120 | struct versionStamp stamp; |
| 121 | ec = ReadHdr1(V_linkHandle(vp)((vp)->linkHandle), (char *)&stamp, sizeof(stamp), |
| 122 | LINKTABLEMAGIC0x99877712, LINKTABLEVERSION1); |
| 123 | } |
| 124 | #endif |
| 125 | if (ec) |
| 126 | return (Volume *) 0; |
| 127 | return vp; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static int |
| 132 | handleit(struct cmd_syndesc *as, void *arock) |
| 133 | { |
| 134 | struct cmd_item *ti; |
| 135 | int err = 0; |
| 136 | afs_uint32 volumeId = 0; |
| 137 | char *partName = 0; |
| 138 | char *fileName = NULL((void *)0); |
| 139 | struct DiskPartition64 *partP = NULL((void *)0); |
| 140 | char name1[128]; |
| 141 | char tmpPartName[20]; |
| 142 | int fromtime = 0; |
| 143 | afs_int32 code; |
| 144 | |
| 145 | |
| 146 | #ifndef AFS_NT40_ENV |
| 147 | #if 0 |
| 148 | if (geteuid() != 0) { |
| 149 | fprintf(stderr__stderrp, "voldump must be run as root; sorry\n"); |
| 150 | exit(1); |
| 151 | } |
| 152 | #endif |
| 153 | #endif |
| 154 | |
| 155 | if ((ti = as->parms[0].items)) |
| 156 | partName = ti->data; |
| 157 | if ((ti = as->parms[1].items)) |
| 158 | volumeId = (afs_uint32)atoi(ti->data); |
| 159 | if ((ti = as->parms[2].items)) |
| 160 | fileName = ti->data; |
| 161 | if ((ti = as->parms[3].items)) |
| 162 | verbose = 1; |
| 163 | if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) { |
| 164 | code = ktime_DateToInt32(as->parms[4].items->data, &fromtime); |
| 165 | if (code) { |
| 166 | fprintf(STDERR__stderrp, "failed to parse date '%s' (error=%d))\n", |
| 167 | as->parms[4].items->data, code); |
| 168 | return code; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | DInit(10); |
| 173 | |
| 174 | err = VAttachPartitions(); |
| 175 | if (err) { |
| 176 | fprintf(stderr__stderrp, "%d partitions had errors during attach.\n", err); |
| 177 | } |
| 178 | |
| 179 | if (partName) { |
| 180 | if (strlen(partName) == 1) { |
| 181 | if (partName[0] >= 'a' && partName[0] <= 'z') { |
| 182 | strcpy(tmpPartName, "/vicepa"); |
| 183 | tmpPartName[6] = partName[0]; |
| 184 | partP = VGetPartition(tmpPartName, 0); |
| 185 | } |
| 186 | } else { |
| 187 | partP = VGetPartition(partName, 0); |
| 188 | } |
| 189 | if (!partP) { |
| 190 | fprintf(stderr__stderrp, |
| 191 | "%s is not an AFS partition name on this server.\n", |
| 192 | partName); |
| 193 | exit(1); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (!volumeId) { |
| 198 | fprintf(stderr__stderrp, "Must specify volume id!\n"); |
| 199 | exit(1); |
| 200 | } |
| 201 | |
| 202 | if (!partP) { |
| 203 | fprintf(stderr__stderrp, "must specify vice partition.\n"); |
| 204 | exit(1); |
| 205 | } |
| 206 | |
| 207 | snprintf(name1, sizeof name1, VFORMAT"V%010lu.vol", (unsigned long)volumeId); |
| 208 | HandleVolume(partP, name1, fileName, fromtime); |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | HandleVolume(struct DiskPartition64 *dp, char *name, char *filename, int fromtime) |
| 214 | { |
| 215 | struct VolumeHeader header; |
| 216 | struct VolumeDiskHeader diskHeader; |
| 217 | struct afs_statstat status; |
| 218 | int fd; |
| 219 | Volume *vp; |
| 220 | char headerName[1024]; |
| 221 | |
| 222 | afs_int32 n; |
| 223 | |
| 224 | snprintf(headerName, sizeof headerName, "%s" OS_DIRSEP"/" "%s", |
| 225 | VPartitionPath(dp), name); |
| 226 | if ((fd = afs_openopen(headerName, O_RDONLY0x0000)) == -1 |
| 227 | || afs_fstatfstat(fd, &status) == -1) { |
| 228 | fprintf(stderr__stderrp, "Cannot read volume header %s\n", name); |
| 229 | close(fd); |
| 230 | exit(1); |
| 231 | } |
| 232 | n = read(fd, &diskHeader, sizeof(diskHeader)); |
| 233 | |
| 234 | if (n != sizeof(diskHeader) |
| 235 | || diskHeader.stamp.magic != VOLUMEHEADERMAGIC((bit32)0x88a1bb3c)) { |
| 236 | fprintf(stderr__stderrp, "Error reading volume header %s\n", name); |
| 237 | exit(1); |
| 238 | } |
| 239 | if (diskHeader.stamp.version != VOLUMEHEADERVERSION1) { |
| 240 | fprintf(stderr__stderrp, |
| 241 | "Volume %s, version number is incorrect; volume needs salvage\n", |
| 242 | name); |
| 243 | exit(1); |
| 244 | } |
| 245 | DiskToVolumeHeader(&header, &diskHeader); |
| 246 | |
| 247 | close(fd); |
| 248 | vp = AttachVolume(dp, name, &header); |
| 249 | if (!vp) { |
| 250 | fprintf(stderr__stderrp, "Error attaching volume header %s\n", name); |
| 251 | exit(1); |
| 252 | } |
| 253 | |
| 254 | DoMyVolDump(vp, dp, filename, fromtime); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | int |
| 259 | main(int argc, char **argv) |
| 260 | { |
| 261 | struct cmd_syndesc *ts; |
| 262 | afs_int32 code; |
| 263 | VolumePackageOptions opts; |
| 264 | |
| 265 | VOptDefaults(volumeUtility, &opts); |
| 266 | if (VInitVolumePackage2(volumeUtility, &opts)) { |
| 267 | fprintf(stderr__stderrp, "errors encountered initializing volume package, but " |
| 268 | "trying to continue anyway\n"); |
| 269 | } |
| 270 | |
| 271 | ts = cmd_CreateSyntax(NULL((void *)0), handleit, NULL((void *)0), |
| 272 | "Dump a volume to a 'vos dump' format file without using volserver"); |
| 273 | cmd_AddParm(ts, "-part", CMD_LIST3, CMD_OPTIONAL1, "AFS partition name"); |
| 274 | cmd_AddParm(ts, "-volumeid", CMD_LIST3, CMD_OPTIONAL1, "Volume id"); |
| 275 | cmd_AddParm(ts, "-file", CMD_LIST3, CMD_OPTIONAL1, "Dump filename"); |
| 276 | cmd_AddParm(ts, "-verbose", CMD_FLAG1, CMD_OPTIONAL1, |
| 277 | "Trace dump progress (very verbose)"); |
| 278 | cmd_AddParm(ts, "-time", CMD_SINGLE2, CMD_OPTIONAL1, "dump from time"); |
| 279 | code = cmd_Dispatch(argc, argv); |
| 280 | return code; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | static int |
| 287 | DumpDouble(int dumpfd, char tag, afs_uint32 value1, |
| 288 | afs_uint32 value2) |
| 289 | { |
| 290 | int res; |
| 291 | char tbuffer[9]; |
| 292 | byte *p = (unsigned char *)tbuffer; |
| 293 | *p++ = tag; |
| 294 | afs_putint32(p, value1)*p++ = value1>>24, *p++ = value1>>16, *p++ = value1 >>8, *p++ = value1; |
| 295 | afs_putint32(p, value2)*p++ = value2>>24, *p++ = value2>>16, *p++ = value2 >>8, *p++ = value2; |
| 296 | |
| 297 | res = write(dumpfd, tbuffer, 9); |
| 298 | return ((res == 9) ? 0 : VOLSERDUMPERROR(1492325123L)); |
| 299 | } |
| 300 | |
| 301 | static int |
| 302 | DumpInt32(int dumpfd, char tag, afs_uint32 value) |
| 303 | { |
| 304 | char tbuffer[5]; |
| 305 | byte *p = (unsigned char *)tbuffer; |
| 306 | *p++ = tag; |
| 307 | afs_putint32(p, value)*p++ = value>>24, *p++ = value>>16, *p++ = value>> 8, *p++ = value; |
| 308 | return ((write(dumpfd, tbuffer, 5) == 5) ? 0 : VOLSERDUMPERROR(1492325123L)); |
| 309 | } |
| 310 | |
| 311 | static int |
| 312 | DumpString(int dumpfd, char tag, char *s) |
| 313 | { |
| 314 | int n; |
| 315 | int code = 0; |
| 316 | code = write(dumpfd, &tag, 1); |
| 317 | if (code != 1) |
| 318 | return VOLSERDUMPERROR(1492325123L); |
| 319 | n = strlen(s) + 1; |
| 320 | code = write(dumpfd, s, n); |
| 321 | if (code != n) |
| 322 | return VOLSERDUMPERROR(1492325123L); |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | static int |
| 328 | DumpArrayInt32(int dumpfd, char tag, afs_uint32 * array, |
| 329 | int nelem) |
| 330 | { |
| 331 | char tbuffer[4]; |
| 332 | afs_uint32 v; |
| 333 | int code = 0; |
| 334 | byte *p = (unsigned char *)tbuffer; |
| 335 | *p++ = tag; |
| 336 | afs_putshort(p, nelem)*p++ = nelem>>8, *p++ = nelem; |
| 337 | code = write(dumpfd, tbuffer, 3); |
| 338 | if (code != 3) |
| 339 | return VOLSERDUMPERROR(1492325123L); |
| 340 | while (nelem--) { |
| 341 | p = (unsigned char *)tbuffer; |
| 342 | v = *array++; /*this was register */ |
| 343 | |
| 344 | afs_putint32(p, v)*p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p ++ = v; |
| 345 | code = write(dumpfd, tbuffer, 4); |
| 346 | if (code != 4) |
| 347 | return VOLSERDUMPERROR(1492325123L); |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | static int |
| 356 | DumpDumpHeader(int dumpfd, Volume * vp, afs_int32 fromtime) |
| 357 | { |
| 358 | int code = 0; |
| 359 | afs_int32 dumpTimes[2]; |
| 360 | |
| 361 | if (verbose) |
| 362 | fprintf(stderr__stderrp, "dumping dump header\n"); |
| 363 | |
| 364 | if (!code) |
| 365 | code = DumpDouble(dumpfd, D_DUMPHEADER1, DUMPBEGINMAGIC0xB3A11322, DUMPVERSION1); |
| 366 | |
| 367 | if (!code) |
| 368 | code = DumpInt32(dumpfd, 'v', V_id(vp)((vp)->header->diskstuff.id)); |
| 369 | |
| 370 | if (!code) |
| 371 | code = DumpString(dumpfd, 'n', V_name(vp)((vp)->header->diskstuff.name)); |
| 372 | |
| 373 | dumpTimes[0] = fromtime; |
| 374 | dumpTimes[1] = V_backupDate(vp)((vp)->header->diskstuff.backupDate); /* Until the time the clone was made */ |
| 375 | if (!code) |
| 376 | code = DumpArrayInt32(dumpfd, 't', (afs_uint32 *) dumpTimes, 2); |
| 377 | |
| 378 | return code; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | static int |
| 383 | DumpEnd(int dumpfd) |
| 384 | { |
| 385 | return (DumpInt32(dumpfd, D_DUMPEND4, DUMPENDMAGIC0x3A214B6E)); |
| 386 | } |
| 387 | |
| 388 | static int |
| 389 | DumpByte(int dumpfd, char tag, byte value) |
| 390 | { |
| 391 | char tbuffer[2]; |
| 392 | byte *p = (unsigned char *)tbuffer; |
| 393 | *p++ = tag; |
| 394 | *p = value; |
| 395 | return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR(1492325123L)); |
| 396 | } |
| 397 | |
| 398 | static int |
| 399 | DumpTag(int dumpfd, int tag) |
| 400 | { |
| 401 | char p; |
| 402 | |
| 403 | p = tag; |
| 404 | return ((write(dumpfd, &p, 1) == 1) ? 0 : VOLSERDUMPERROR(1492325123L)); |
| 405 | |
| 406 | } |
| 407 | |
| 408 | static int |
| 409 | DumpBool(int dumpfd, char tag, unsigned int value) |
| 410 | { |
| 411 | char tbuffer[2]; |
| 412 | byte *p = (unsigned char *)tbuffer; |
| 413 | *p++ = tag; |
| 414 | *p = value; |
| 415 | return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR(1492325123L)); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | |
| 420 | static int |
| 421 | DumpVolumeHeader(int dumpfd, Volume * vp) |
| 422 | { |
| 423 | int code = 0; |
| 424 | |
| 425 | if (verbose) |
| 426 | fprintf(stderr__stderrp, "dumping volume header\n"); |
| 427 | |
| 428 | if (!code) |
| 429 | code = DumpTag(dumpfd, D_VOLUMEHEADER2); |
| 430 | if (!code) |
| 431 | code = DumpInt32(dumpfd, 'i', V_id(vp)((vp)->header->diskstuff.id)); |
| 432 | if (!code) |
| 433 | code = DumpInt32(dumpfd, 'v', V_stamp(vp)((vp)->header->diskstuff.stamp).version); |
| 434 | if (!code) |
| 435 | code = DumpString(dumpfd, 'n', V_name(vp)((vp)->header->diskstuff.name)); |
| 436 | if (!code) |
| 437 | code = DumpBool(dumpfd, 's', V_inService(vp)((vp)->header->diskstuff.inService)); |
| 438 | if (!code) |
| 439 | code = DumpBool(dumpfd, 'b', V_blessed(vp)((vp)->header->diskstuff.blessed)); |
| 440 | if (!code) |
| 441 | code = DumpInt32(dumpfd, 'u', V_uniquifier(vp)((vp)->header->diskstuff.uniquifier)); |
| 442 | if (!code) |
| 443 | code = DumpByte(dumpfd, 't', (byte) V_type(vp)((vp)->header->diskstuff.type)); |
| 444 | if (!code) |
| 445 | code = DumpInt32(dumpfd, 'p', V_parentId(vp)((vp)->header->diskstuff.parentId)); |
| 446 | if (!code) |
| 447 | code = DumpInt32(dumpfd, 'c', V_cloneId(vp)((vp)->header->diskstuff.cloneId)); |
| 448 | if (!code) |
| 449 | code = DumpInt32(dumpfd, 'q', V_maxquota(vp)((vp)->header->diskstuff.maxquota)); |
| 450 | if (!code) |
| 451 | code = DumpInt32(dumpfd, 'm', V_minquota(vp)((vp)->header->diskstuff.minquota)); |
| 452 | if (!code) |
| 453 | code = DumpInt32(dumpfd, 'd', V_diskused(vp)((vp)->header->diskstuff.diskused)); |
| 454 | if (!code) |
| 455 | code = DumpInt32(dumpfd, 'f', V_filecount(vp)((vp)->header->diskstuff.filecount)); |
| 456 | if (!code) |
| 457 | code = DumpInt32(dumpfd, 'a', V_accountNumber(vp)((vp)->header->diskstuff.accountNumber)); |
| 458 | if (!code) |
| 459 | code = DumpInt32(dumpfd, 'o', V_owner(vp)((vp)->header->diskstuff.owner)); |
| 460 | if (!code) |
| 461 | code = DumpInt32(dumpfd, 'C', V_creationDate(vp)((vp)->header->diskstuff.creationDate)); /* Rw volume creation date */ |
| 462 | if (!code) |
| 463 | code = DumpInt32(dumpfd, 'A', V_accessDate(vp)((vp)->header->diskstuff.accessDate)); |
| 464 | if (!code) |
| 465 | code = DumpInt32(dumpfd, 'U', V_updateDate(vp)((vp)->header->diskstuff.updateDate)); |
| 466 | if (!code) |
| 467 | code = DumpInt32(dumpfd, 'E', V_expirationDate(vp)((vp)->header->diskstuff.expirationDate)); |
| 468 | if (!code) |
| 469 | code = DumpInt32(dumpfd, 'B', V_backupDate(vp)((vp)->header->diskstuff.backupDate)); /* Rw volume backup clone date */ |
| 470 | if (!code) |
| 471 | code = DumpString(dumpfd, 'O', V_offlineMessage(vp)((vp)->header->diskstuff.offlineMessage)); |
| 472 | |
| 473 | /* |
| 474 | * We do NOT dump the detailed volume statistics residing in the old |
| 475 | * motd field, since we cannot tell from the info in a dump whether |
| 476 | * statistics data has been put there. Instead, we dump a null string, |
| 477 | * just as if that was what the motd contained. |
| 478 | */ |
| 479 | if (!code) |
| 480 | code = DumpString(dumpfd, 'M', ""); |
| 481 | if (!code) |
| 482 | code = |
| 483 | DumpArrayInt32(dumpfd, 'W', (afs_uint32 *) V_weekUse(vp)((vp)->header->diskstuff.weekUse), |
| 484 | sizeof(V_weekUse(vp)((vp)->header->diskstuff.weekUse)) / sizeof(V_weekUse(vp)((vp)->header->diskstuff.weekUse)[0])); |
| 485 | if (!code) |
| 486 | code = DumpInt32(dumpfd, 'D', V_dayUseDate(vp)((vp)->header->diskstuff.dayUseDate)); |
| 487 | if (!code) |
| 488 | code = DumpInt32(dumpfd, 'Z', V_dayUse(vp)((vp)->header->diskstuff.dayUse)); |
| 489 | return code; |
| 490 | } |
| 491 | |
| 492 | static int |
| 493 | DumpShort(int dumpfd, char tag, unsigned int value) |
| 494 | { |
| 495 | char tbuffer[3]; |
| 496 | byte *p = (unsigned char *)tbuffer; |
| 497 | *p++ = tag; |
| 498 | *p++ = value >> 8; |
| 499 | *p = value; |
| 500 | return ((write(dumpfd, tbuffer, 3) == 3) ? 0 : VOLSERDUMPERROR(1492325123L)); |
| 501 | } |
| 502 | |
| 503 | static int |
| 504 | DumpByteString(int dumpfd, char tag, byte * bs, int nbytes) |
| 505 | { |
| 506 | int code = 0; |
| 507 | |
| 508 | code = write(dumpfd, &tag, 1); |
| 509 | if (code != 1) |
| 510 | return VOLSERDUMPERROR(1492325123L); |
| 511 | code = write(dumpfd, (char *)bs, nbytes); |
| 512 | if (code != nbytes) |
| 513 | return VOLSERDUMPERROR(1492325123L); |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | |
| 518 | static int |
| 519 | DumpFile(int dumpfd, int vnode, FdHandle_t * handleP, struct VnodeDiskObject *v) |
| 520 | { |
| 521 | int code = 0, failed_seek = 0, failed_write = 0; |
| 522 | afs_int32 pad = 0; |
| 523 | afs_foff_t offset = 0; |
| 524 | afs_sfsize_t nbytes, howBig; |
| 525 | ssize_t n; |
| 526 | size_t howMany; |
| 527 | afs_foff_t howFar = 0; |
| 528 | byte *p; |
| 529 | afs_uint32 hi, lo; |
| 530 | afs_ino_str_t stmp; |
| 531 | #ifndef AFS_NT40_ENV |
| 532 | struct afs_statstat status; |
| 533 | #endif |
| 534 | afs_sfsize_t size; |
| 535 | #ifdef AFS_AIX_ENV |
| 536 | #include <sys/statfs.h> |
| 537 | struct statfs tstatfs; |
| 538 | #endif |
| 539 | |
| 540 | if (verbose) |
| 541 | fprintf(stderr__stderrp, "dumping file for vnode %d\n", vnode); |
| 542 | |
| 543 | #ifdef AFS_NT40_ENV |
| 544 | howBig = _filelength(handleP->fd_fd); |
| 545 | howMany = 4096; |
| 546 | |
| 547 | #else |
| 548 | afs_fstatfstat(handleP->fd_fd, &status); |
| 549 | howBig = status.st_size; |
| 550 | |
| 551 | #ifdef AFS_AIX_ENV |
| 552 | /* Unfortunately in AIX valuable fields such as st_blksize are |
| 553 | * gone from the stat structure. |
| 554 | */ |
| 555 | fstatfs(handleP->fd_fd, &tstatfs); |
| 556 | howMany = tstatfs.f_bsize; |
| 557 | #else |
| 558 | howMany = status.st_blksize; |
| 559 | #endif /* AFS_AIX_ENV */ |
| 560 | #endif /* AFS_NT40_ENV */ |
| 561 | |
| 562 | |
| 563 | size = FDH_SIZE(handleP)ih_size((handleP)->fd_fd); |
| 564 | |
| 565 | if (verbose) |
| 566 | fprintf(stderr__stderrp, " howBig = %u, howMany = %u, fdh size = %u\n", |
| 567 | (unsigned int) howBig, (unsigned int) howMany, |
| 568 | (unsigned int) size); |
| 569 | |
| 570 | SplitInt64(size, hi, lo)(hi) = ((afs_int64)size) >> 32; (lo) = (size) & 0xFFFFFFFF ;; |
| 571 | if (hi == 0L) { |
| 572 | code = DumpInt32(dumpfd, 'f', lo); |
| 573 | } else { |
| 574 | code = DumpDouble(dumpfd, 'h', hi, lo); |
| 575 | } |
| 576 | |
| 577 | if (code) { |
| 578 | return VOLSERDUMPERROR(1492325123L); |
| 579 | } |
| 580 | |
| 581 | p = (unsigned char *)malloc(howMany); |
| 582 | if (!p) { |
| 583 | fprintf(stderr__stderrp, "out of memory!\n"); |
| 584 | return VOLSERDUMPERROR(1492325123L); |
| 585 | } |
| 586 | |
| 587 | /* loop through whole file, while we still have bytes left, and no errors, in chunks of howMany bytes */ |
| 588 | for (nbytes = size; (nbytes && !failed_write); nbytes -= howMany) { |
| 589 | if (nbytes < howMany) |
| 590 | howMany = nbytes; |
| 591 | |
| 592 | /* Read the data - unless we know we can't */ |
| 593 | n = (failed_seek ? 0 : FDH_PREAD(handleP, p, howMany, howFar)pread((handleP)->fd_fd, p, howMany, howFar)); |
| 594 | howFar += n; |
| 595 | |
| 596 | /* If read any good data and we null padded previously, log the |
| 597 | * amount that we had null padded. |
| 598 | */ |
| 599 | if ((n > 0) && pad) { |
| 600 | fprintf(stderr__stderrp, "Null padding file %d bytes at offset %lld\n", pad, |
| 601 | (long long)offset); |
| 602 | pad = 0; |
| 603 | } |
| 604 | |
| 605 | /* If didn't read enough data, null padd the rest of the buffer. This |
| 606 | * can happen if, for instance, the media has some bad spots. We don't |
| 607 | * want to quit the dump, so we start null padding. |
| 608 | */ |
| 609 | if (n < howMany) { |
| 610 | |
| 611 | if (verbose) fprintf(stderr__stderrp, " read %u instead of %u bytes.\n", (unsigned)n, (unsigned)howMany); |
| 612 | |
| 613 | /* Record the read error */ |
| 614 | if (n < 0) { |
| 615 | n = 0; |
| 616 | fprintf(stderr__stderrp, "Error %d reading inode %s for vnode %d\n", |
| 617 | errno(* __error()), PrintInode(stmp, handleP->fd_ih->ih_ino), |
| 618 | vnode); |
| 619 | } else if (!pad) { |
| 620 | fprintf(stderr__stderrp, "Error reading inode %s for vnode %d\n", |
| 621 | PrintInode(stmp, handleP->fd_ih->ih_ino), vnode); |
| 622 | } |
| 623 | |
| 624 | /* Pad the rest of the buffer with zeros. Remember offset we started |
| 625 | * padding. Keep total tally of padding. |
| 626 | */ |
| 627 | memset(p + n, 0, howMany - n); |
| 628 | if (!pad) |
| 629 | offset = (howBig - nbytes) + n; |
| 630 | pad += (howMany - n); |
| 631 | |
| 632 | /* Now seek over the data we could not get. An error here means we |
| 633 | * can't do the next read. |
| 634 | */ |
| 635 | howFar = ((size - nbytes) + howMany); |
| 636 | } |
| 637 | |
| 638 | /* Now write the data out */ |
| 639 | if (write(dumpfd, (char *)p, howMany) != howMany) |
| 640 | failed_write = VOLSERDUMPERROR(1492325123L); |
| 641 | } |
| 642 | |
| 643 | if (pad) { /* Any padding we hadn't reported yet */ |
| 644 | fprintf(stderr__stderrp, "Null padding file: %d bytes at offset %lld\n", pad, |
| 645 | (long long)offset); |
| 646 | } |
| 647 | |
| 648 | free(p); |
| 649 | return failed_write; |
| 650 | } |
| 651 | |
| 652 | |
| 653 | static int |
| 654 | DumpVnode(int dumpfd, struct VnodeDiskObject *v, int volid, int vnodeNumber, |
| 655 | int dumpEverything, struct Volume *vp) |
| 656 | { |
| 657 | int code = 0; |
| 658 | IHandle_t *ihP; |
| 659 | FdHandle_t *fdP; |
| 660 | afs_ino_str_t stmp; |
| 661 | |
| 662 | if (verbose) |
| 663 | fprintf(stderr__stderrp, "dumping vnode %d\n", vnodeNumber); |
| 664 | |
| 665 | if (!v || v->type == vNull0) |
| 666 | return code; |
| 667 | if (!code) |
| 668 | code = DumpDouble(dumpfd, D_VNODE3, vnodeNumber, v->uniquifier); |
| 669 | if (!dumpEverything) |
| 670 | return code; |
| 671 | if (!code) |
| 672 | code = DumpByte(dumpfd, 't', (byte) v->type); |
| 673 | if (!code) |
| 674 | code = DumpShort(dumpfd, 'l', v->linkCount); /* May not need this */ |
| 675 | if (!code) |
| 676 | code = DumpInt32(dumpfd, 'v', v->dataVersion); |
| 677 | if (!code) |
| 678 | code = DumpInt32(dumpfd, 'm', v->unixModifyTime); |
| 679 | if (!code) |
| 680 | code = DumpInt32(dumpfd, 'a', v->author); |
| 681 | if (!code) |
| 682 | code = DumpInt32(dumpfd, 'o', v->owner); |
| 683 | if (!code && v->group) |
| 684 | code = DumpInt32(dumpfd, 'g', v->group); /* default group is 0 */ |
| 685 | if (!code) |
| 686 | code = DumpShort(dumpfd, 'b', v->modeBits); |
| 687 | if (!code) |
| 688 | code = DumpInt32(dumpfd, 'p', v->parent); |
| 689 | if (!code) |
| 690 | code = DumpInt32(dumpfd, 's', v->serverModifyTime); |
| 691 | if (v->type == vDirectory2) { |
| 692 | acl_HtonACL(VVnodeDiskACL(v)((AL_AccessList *) (((byte *)(v))+64))); |
| 693 | if (!code) |
| 694 | code = |
| 695 | DumpByteString(dumpfd, 'A', (byte *) VVnodeDiskACL(v)((AL_AccessList *) (((byte *)(v))+64)), |
| 696 | VAclDiskSize(v)(256 - 64)); |
| 697 | } |
| 698 | |
| 699 | if (VNDISK_GET_INO(v)((Inode)((v)->vn_ino_lo | ((v)->vn_ino_hi ? (((Inode)(v )->vn_ino_hi)<<32) : 0)))) { |
| 700 | IH_INIT(ihP, V_device(vp), V_parentId(vp), VNDISK_GET_INO(v))((ihP) = ih_init((((vp)->device)), (((vp)->header->diskstuff .parentId)), (((Inode)((v)->vn_ino_lo | ((v)->vn_ino_hi ? (((Inode)(v)->vn_ino_hi)<<32) : 0)))))); |
| 701 | fdP = IH_OPEN(ihP)ih_open(ihP); |
| 702 | if (fdP == NULL((void *)0)) { |
| 703 | fprintf(stderr__stderrp, |
| 704 | "Unable to open inode %s for vnode %u (volume %i); not dumped, error %d\n", |
| 705 | PrintInode(stmp, VNDISK_GET_INO(v)((Inode)((v)->vn_ino_lo | ((v)->vn_ino_hi ? (((Inode)(v )->vn_ino_hi)<<32) : 0)))), vnodeNumber, volid, |
| 706 | errno(* __error())); |
| 707 | } |
| 708 | else |
| 709 | { |
| 710 | if (verbose) |
| 711 | fprintf(stderr__stderrp, "about to dump inode %s for vnode %u\n", |
| 712 | PrintInode(stmp, VNDISK_GET_INO(v)((Inode)((v)->vn_ino_lo | ((v)->vn_ino_hi ? (((Inode)(v )->vn_ino_hi)<<32) : 0)))), vnodeNumber); |
| 713 | code = DumpFile(dumpfd, vnodeNumber, fdP, v); |
| 714 | FDH_CLOSE(fdP)(fd_close(fdP), (fdP)=((void *)0), 0); |
| 715 | } |
| 716 | IH_RELEASE(ihP)(ih_release(ihP), (ihP)=((void *)0), 0); |
| 717 | } |
| 718 | |
| 719 | if (verbose) |
| 720 | fprintf(stderr__stderrp, "done dumping vnode %d\n", vnodeNumber); |
| 721 | return code; |
| 722 | } |
| 723 | |
| 724 | |
| 725 | static int |
| 726 | DumpVnodeIndex(int dumpfd, Volume * vp, VnodeClass class, afs_int32 fromtime, |
| 727 | int forcedump) |
| 728 | { |
| 729 | int code = 0; |
| 730 | struct VnodeClassInfo *vcp = &VnodeClassInfo[class]; |
| 731 | char buf[SIZEOF_LARGEDISKVNODE256]; |
| 732 | struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf; |
| 733 | StreamHandle_t *file; |
| 734 | FdHandle_t *fdP; |
| 735 | afs_sfsize_t size; |
| 736 | int flag; |
| 737 | afs_foff_t offset = 0; |
| 738 | int vnodeIndex, nVnodes = 0; |
| 739 | |
| 740 | fdP = IH_OPEN(vp->vnodeIndex[class].handle)ih_open(vp->vnodeIndex[class].handle); |
| 741 | file = FDH_FDOPEN(fdP, "r+")stream_fdopen((fdP)->fd_fd); |
| 742 | size = OS_SIZE(fdP->fd_fd)ih_size(fdP->fd_fd); |
| 743 | nVnodes = (size / vcp->diskSize) - 1; |
| 744 | |
| 745 | if (nVnodes > 0) { |
| 746 | STREAM_ASEEK(file, vcp->diskSize)stream_aseek(file, vcp->diskSize); |
| 747 | } else |
| 748 | nVnodes = 0; |
| 749 | for (vnodeIndex = 0; |
| 750 | nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file)stream_read(vnode, vcp->diskSize, 1, file) == 1 && !code; |
| 751 | nVnodes--, vnodeIndex++, offset += vcp->diskSize) { |
| 752 | flag = forcedump || (vnode->serverModifyTime >= fromtime); |
| 753 | /* Note: the >= test is very important since some old volumes may not have |
| 754 | * a serverModifyTime. For an epoch dump, this results in 0>=0 test, which |
| 755 | * does dump the file! */ |
| 756 | if (verbose) |
| 757 | fprintf(stderr__stderrp, "about to dump %s vnode %u (vnode offset = %lld)\n", |
| 758 | class == vSmall1 ? "vSmall" : "vLarge", |
| 759 | bitNumberToVnodeNumber(vnodeIndex, class)((VnodeId)(((vnodeIndex)<<1)+(class)+1)), (long long)offset); |
| 760 | if (!code) |
| 761 | code = |
| 762 | DumpVnode(dumpfd, vnode, V_id(vp)((vp)->header->diskstuff.id), |
| 763 | bitNumberToVnodeNumber(vnodeIndex, class)((VnodeId)(((vnodeIndex)<<1)+(class)+1)), flag, |
| 764 | vp); |
| 765 | } |
| 766 | STREAM_CLOSE(file)stream_close(file, 0); |
| 767 | FDH_CLOSE(fdP)(fd_close(fdP), (fdP)=((void *)0), 0); |
| 768 | return code; |
| 769 | } |
| 770 | |
| 771 | |
| 772 | |
| 773 | /* A partial dump (no dump header) */ |
| 774 | static int |
| 775 | DumpPartial(int dumpfd, Volume * vp, afs_int32 fromtime, |
| 776 | int dumpAllDirs) |
| 777 | { |
| 778 | int code = 0; |
| 779 | |
| 780 | if (verbose) |
| 781 | fprintf(stderr__stderrp, "about to dump the volume header\n"); |
| 782 | if (!code) |
| 783 | code = DumpVolumeHeader(dumpfd, vp); |
| 784 | |
| 785 | if (verbose) |
| 786 | fprintf(stderr__stderrp, "about to dump the large vnode index\n"); |
| 787 | if (!code) |
| 788 | code = DumpVnodeIndex(dumpfd, vp, vLarge0, fromtime, dumpAllDirs); |
| 789 | |
| 790 | if (verbose) |
| 791 | fprintf(stderr__stderrp, "about to dump the small vnode index\n"); |
| 792 | if (!code) |
| 793 | code = DumpVnodeIndex(dumpfd, vp, vSmall1, fromtime, 0); |
| 794 | return code; |
| 795 | } |
| 796 | |
| 797 | |
| 798 | |
| 799 | static void |
| 800 | DoMyVolDump(Volume * vp, struct DiskPartition64 *dp, char *dumpfile, int fromtime) |
| 801 | { |
| 802 | int code = 0; |
| 803 | int dumpAllDirs = 0; |
| 804 | int dumpfd = 0; |
| 805 | |
| 806 | if (dumpfile) { |
| 807 | unlink(dumpfile); |
| 808 | dumpfd = |
| 809 | afs_openopen(dumpfile, O_CREAT0x0200 | O_WRONLY0x0001 | O_TRUNC0x0400, S_IRUSR0000400 | S_IWUSR0000200); |
| 810 | if (dumpfd < 0) { |
| 811 | fprintf(stderr__stderrp, "Failed to open dump file: %s. Exiting.\n", |
| 812 | afs_error_message(errno(* __error()))); |
| 813 | exit(1); |
| 814 | } |
| 815 | } else { |
| 816 | dumpfd = 1; /* stdout */ |
| 817 | } |
| 818 | |
| 819 | if (verbose) |
| 820 | fprintf(stderr__stderrp, "about to dump the dump header\n"); |
| 821 | if (!code) |
| 822 | code = DumpDumpHeader(dumpfd, vp, fromtime); |
| 823 | |
| 824 | if (verbose) |
| 825 | fprintf(stderr__stderrp, "about to dump volume contents\n"); |
| 826 | if (!code) |
| 827 | code = DumpPartial(dumpfd, vp, fromtime, dumpAllDirs); |
| 828 | |
| 829 | if (verbose) |
| 830 | fprintf(stderr__stderrp, "about to dump the dump postamble\n"); |
| 831 | if (!code) |
| 832 | code = DumpEnd(dumpfd); |
Value stored to 'code' is never read | |
| 833 | |
| 834 | if (verbose) |
| 835 | fprintf(stderr__stderrp, "finished dump\n"); |
| 836 | close(dumpfd); /* might be closing stdout, no harm */ |
| 837 | } |