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
|
/*
* Copyright (c) 2000-2002 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
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include <uuid/uuid.h>
#include "config.h"
#include "types.h"
#include "mlog.h"
#include "dlog.h"
#include "path.h"
#include "getopt.h"
#include "global.h"
#include "drive.h"
/* drive.c - selects and initializes a drive strategy
*/
/* structure definitions used locally ****************************************/
/* declarations of externally defined global symbols *************************/
extern void usage(void);
extern char *homedir;
/* declare all drive strategies here
*/
extern drive_strategy_t drive_strategy_simple;
extern drive_strategy_t drive_strategy_scsitape;
extern drive_strategy_t drive_strategy_rmt;
/* forward declarations of locally defined static functions ******************/
static drive_t *drive_alloc(char *, size_t);
static void drive_allochdrs(drive_t *drivep,
global_hdr_t *gwhdrtemplatep,
ix_t driveix);
/* definition of locally defined global variables ****************************/
drive_t **drivepp;
size_t drivecnt;
size_t partialmax;
/* definition of locally defined static variables *****************************/
/* drive strategy array - ordered by precedence
*/
static drive_strategy_t *strategypp[] = {
&drive_strategy_simple,
&drive_strategy_scsitape,
&drive_strategy_rmt,
};
/* definition of locally defined global functions ****************************/
/* drive_init1 - select and instantiate a drive manager for each drive
* specified on the command line.
*/
bool_t
drive_init1(int argc, char *argv[])
{
int c;
ix_t driveix;
/* sanity check asserts
*/
assert(sizeof(drive_hdr_t) == DRIVE_HDR_SZ);
/* count drive arguments
*/
optind = 1;
opterr = 0;
drivecnt = 0;
while ((c = getopt(argc, argv, GETOPT_CMDSTRING)) != EOF) {
switch (c) {
case GETOPT_DUMPDEST:
drivecnt++;
break;
}
}
/* allocate an array to hold ptrs to drive descriptors
*/
if (drivecnt > 0) {
drivepp = (drive_t **)calloc(drivecnt, sizeof(drive_t *));
assert(drivepp);
}
/* initialize the partialmax value. Each drive can be completing a file
* started in another drive (except for drive 0) and leave one file to
* be completed by another drive. This value is used to limit the
* search in the list of partially completed files shared between all
* restore streams. Note, if drivecnt is one, then partialmax is zero
* to indicate no partial files can span streams.
*/
partialmax = (drivecnt <= 1 ? 0 : (drivecnt * 2) - 1);
/* initialize drive descriptors from command line arguments
*/
optind = 1;
opterr = 0;
driveix = 0;
while ((c = getopt(argc, argv, GETOPT_CMDSTRING)) != EOF) {
switch (c) {
case GETOPT_DUMPDEST:
if (!optarg || optarg[0] == '-') {
mlog(MLOG_NORMAL,
_("-%c argument missing\n"),
c);
usage();
return BOOL_FALSE;
}
/* allocate a drive descriptor
*/
drivepp[driveix] = drive_alloc(optarg, driveix);
driveix++;
break;
}
}
assert(driveix == drivecnt);
/* the user may specify stdin as the source, by
* a single dash ('-') with no option letter. This must appear
* between all lettered arguments and the file system pathname.
*/
if (optind < argc && !strcmp(argv[optind ], "-")) {
if (driveix > 0) {
mlog(MLOG_NORMAL,
#ifdef DUMP
_("cannot specify source files and stdout together\n")
#endif /* DUMP */
#ifdef RESTORE
_("cannot specify source files and stdin together\n")
#endif /* RESTORE */
);
usage();
return BOOL_FALSE;
}
drivecnt = 1;
/* Adding this alloc to fix malloc corruption.
* Bug #393618 - prasadb 04/16/97
* allocate an array to hold ptrs to drive descriptors
*/
drivepp = (drive_t **)calloc(drivecnt, sizeof(drive_t *));
assert(drivepp);
drivepp[0 ] = drive_alloc("stdio", 0);
#ifdef DUMP /* ifdef added around dlog_desist() by prasadb to fix 435626 */
dlog_desist();
#endif
}
/* verify that some dump destination(s) / restore source(s) specified
*/
if (drivecnt == 0) {
mlog(MLOG_NORMAL | MLOG_ERROR,
#ifdef DUMP
_("no destination file(s) specified\n")
#endif /* DUMP */
#ifdef RESTORE
_("no source file(s) specified\n")
#endif /* RESTORE */
);
usage();
return BOOL_FALSE;
}
/* run each drive past each strategy, pick the best match
* and instantiate a drive manager.
*/
for (driveix = 0; driveix < drivecnt; driveix++) {
drive_t *drivep = drivepp[driveix];
int bestscore = 0 - INTGENMAX;
ix_t six;
ix_t scnt = sizeof(strategypp) / sizeof(strategypp[0]);
drive_strategy_t *bestsp = 0;
bool_t ok;
for (six = 0; six < scnt; six++) {
drive_strategy_t *sp = strategypp[six];
int score;
score = (*sp->ds_match)(argc,
argv,
drivep);
if (!bestsp || score > bestscore) {
bestsp = sp;
bestscore = score;
}
}
assert(bestsp);
drivep->d_strategyp = bestsp;
drivep->d_recmarksep = bestsp->ds_recmarksep;
drivep->d_recmfilesz = bestsp->ds_recmfilesz;
mlog(MLOG_VERBOSE,
_("using %s strategy\n"),
bestsp->ds_description);
ok = (*bestsp->ds_instantiate)(argc,
argv,
drivep);
if (!ok) {
return BOOL_FALSE;
}
}
return BOOL_TRUE;
}
/* drive_init2 - second phase strategy initialization.
* allocates global read and write hdrs, copying global hdr template
* into the write hdrs (DUMP only). kicks off async init for each drive,
* which will be synchronized with drive_init3.
*/
/* ARGSUSED */
bool_t
drive_init2(int argc,
char *argv[],
global_hdr_t *gwhdrtemplatep)
{
ix_t driveix;
for (driveix = 0; driveix < drivecnt; driveix++) {
drive_t *drivep = drivepp[driveix];
bool_t ok;
drive_allochdrs(drivep, gwhdrtemplatep, driveix);
ok = (*drivep->d_opsp->do_init)(drivep);
if (!ok) {
return BOOL_FALSE;
}
}
return BOOL_TRUE;
}
/* drive_init3 - third phase strategy initialization.
* synchronizes with async operations begun by drive_init2.
*/
bool_t
drive_init3(void)
{
ix_t driveix;
for (driveix = 0; driveix < drivecnt; driveix++) {
drive_t *drivep = drivepp[driveix];
bool_t ok;
ok = (*drivep->d_opsp->do_sync)(drivep);
if (!ok) {
return BOOL_FALSE;
}
}
return BOOL_TRUE;
}
/* drive_mark_commit - commits and unlinks all accumulated marks with
* offsets less than or equal to the offset of the next (as yet unwritten)
* byte in the media file.
* utility function for use by drive-specific strategies.
*/
void
drive_mark_commit(drive_t *drivep, off64_t ncommitted)
{
drive_markrec_t *dmp;
for (dmp = drivep->d_markrecheadp
;
dmp && dmp->dm_log <= (drive_mark_t)ncommitted
;
) {
drivep->d_markrecheadp = dmp->dm_nextp;
(*dmp->dm_cbfuncp)(dmp->dm_cbcontextp, dmp, BOOL_TRUE);
dmp = drivep->d_markrecheadp;
}
}
/* drive_mark_discard - unlinks all accumulated marks, calling their callbacks
* indicating the mark was NOT committed.
* utility function for use by drive-specific strategies.
*/
void
drive_mark_discard(drive_t *drivep)
{
drive_markrec_t *dmp;
for (dmp = drivep->d_markrecheadp
;
dmp
;
drivep->d_markrecheadp = dmp->dm_nextp, dmp = dmp->dm_nextp) {
(*dmp->dm_cbfuncp)(dmp->dm_cbcontextp, dmp, BOOL_FALSE);
}
}
/* drive_display_metrics - called by main thread during interactive dialog
* to print drive throughput and streaming metrics.
*/
void
drive_display_metrics(void)
{
ix_t driveix;
for (driveix = 0; driveix < drivecnt; driveix++) {
drive_t *drivep = drivepp[driveix];
drive_ops_t *dop = drivep->d_opsp;
if (dop->do_display_metrics) {
(*dop->do_display_metrics)(drivep);
}
}
}
/* definition of locally defined static functions ****************************/
/* drive_alloc - allocate and initialize the generic portions of a drive
* descriptor. do NOT allocate hdr buffers.
*/
static drive_t *
drive_alloc(char *pathname, ix_t driveix)
{
drive_t *drivep;
struct stat64 statbuf;
/* allocate the descriptor
*/
drivep = (drive_t *)calloc(1, sizeof(drive_t));
assert(drivep);
/* convert the pathname to an absolute pathname
* NOTE: string "stdio" is reserved to mean send to standard out
*/
if (strcmp(pathname, "stdio")) {
pathname = path_reltoabs(pathname, homedir);
}
/* set pipe flags
*/
if (!strcmp(pathname, "stdio")) {
drivep->d_isunnamedpipepr = BOOL_TRUE;
} else if (!stat64(pathname, &statbuf)
&&
(statbuf.st_mode & S_IFMT) == S_IFIFO) {
drivep->d_isnamedpipepr = BOOL_TRUE;
}
/* complete the drive manager
*/
drivep->d_pathname = pathname;
drivep->d_index = driveix;
return drivep;
}
/* drive_allochdrs - allocate and initialize the drive read and write
* hdrs, and ptrs into the hdrs.
*/
static void
drive_allochdrs(drive_t *drivep, global_hdr_t *gwhdrtemplatep, ix_t driveix)
{
global_hdr_t *grhdrp;
drive_hdr_t *drhdrp;
global_hdr_t *gwhdrp;
drive_hdr_t *dwhdrp;
/* allocate the read header
*/
grhdrp = (global_hdr_t *)calloc(1, sizeof(global_hdr_t));
assert(grhdrp);
gwhdrp = NULL;
dwhdrp = NULL;
/* calculate pointer to the drive portion of the read header
*/
drhdrp = (drive_hdr_t *)grhdrp->gh_upper;
/* global write hdr used only for dumps. will be NULL for restore
*/
if (gwhdrtemplatep) {
/* allocate the write header
*/
gwhdrp = (global_hdr_t *)calloc(1, sizeof(global_hdr_t));
assert(gwhdrp);
/* copy the template
*/
*gwhdrp = *gwhdrtemplatep;
/* calculate pointer to the drive portion of the read header
*/
dwhdrp = (drive_hdr_t *)gwhdrp->gh_upper;
/* fill in generic drive fields of write hdr
*/
dwhdrp->dh_strategyid = drivep->d_strategyp->ds_id;
dwhdrp->dh_driveix = driveix;
dwhdrp->dh_drivecnt = drivecnt;
}
/* complete the drive manager
*/
drivep->d_greadhdrp = grhdrp;
drivep->d_readhdrp = drhdrp;
drivep->d_gwritehdrp = gwhdrp;
drivep->d_writehdrp = dwhdrp;
}
|