/
ndma_ctrl_media.c
682 lines (538 loc) · 14.1 KB
/
ndma_ctrl_media.c
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
/*
* Copyright (c) 1998,1999,2000
* Traakan, Inc., Los Altos, CA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Project: NDMJOB
* Ident: $Id: $
*
* Description:
*
*/
#include "ndmagents.h"
#ifndef NDMOS_OPTION_NO_CONTROL_AGENT
int
ndmca_media_load_first (struct ndm_session *sess)
{
sess->control_acb->cur_media_ix = 1;
return ndmca_media_load_current (sess);
}
/*
* TODO: It would be nice that if a media entry has a problem
* during load_current() to just skip over it and proceed
* to the next one. It will be really annoying to have a
* long running backup terminate because of a write-protect
* or label-check error when there are perfectly good
* tapes available.
*/
int
ndmca_media_load_next (struct ndm_session *sess)
{
int n_media = sess->control_acb->job.media_tab.n_media;
if (sess->control_acb->cur_media_ix >= n_media) {
ndmalogf (sess, 0, 0, "Out of tapes");
return -1;
}
sess->control_acb->cur_media_ix++;
return ndmca_media_load_current (sess);
}
int
ndmca_media_unload_last (struct ndm_session *sess)
{
return ndmca_media_unload_current(sess);
}
int
ndmca_media_change (struct ndm_session *sess)
{
int rc;
rc = ndmca_media_unload_current(sess);
if (rc) return rc;
rc = ndmca_media_load_next (sess);
if (rc) return rc;
return 0;
}
int
ndmca_media_load_seek (struct ndm_session *sess, unsigned long long pos)
{
struct ndm_control_agent *ca = sess->control_acb;
struct ndm_job_param * job = &ca->job;
int n_media = job->media_tab.n_media;
struct ndmmedia * me;
for (me = job->media_tab.head; me; me = me->next) {
if (me->begin_offset <= pos && pos < me->end_offset)
break;
}
if (!me || me->index > n_media) {
ndmalogf (sess, 0, 0, "Seek to unspecified media");
return -1;
}
ca->cur_media_ix = me->index;
return ndmca_media_load_current (sess);
}
int
ndmca_media_load_current (struct ndm_session *sess)
{
struct ndm_control_agent *ca = sess->control_acb;
struct ndm_job_param * job = &ca->job;
struct ndmmedia * me;
int rc;
unsigned count;
for (me = job->media_tab.head; me; me = me->next) {
if (me->index == ca->cur_media_ix)
break;
}
if (!me) {
return -1;
}
if (job->have_robot) {
rc = ndmca_robot_load (sess, me->slot_addr);
if (rc) return rc;
}
me->media_used = 1;
rc = ndmca_media_open_tape (sess);
/*
* TODO: it would be nice to discriminate this and
* set flags accordingly. For example,
* indicate a write-protect tape.
*/
if (rc) {
me->media_open_error = 1;
/* if use_eject, this won't work */
if (job->have_robot) {
/* best-effort unload the robot */
ndmca_robot_unload (sess, me->slot_addr);
}
return rc;
}
ca->media_is_loaded = 1;
rc = ndmca_media_mtio_tape (sess, NDMP9_MTIO_REW, 1, 0);
if (rc) {
me->media_io_error = 1;
goto close_and_unload;
}
if (ca->is_label_op) {
if (ca->tape_mode == NDMP9_TAPE_RDWR_MODE)
me->media_written = 1; /* most likely */
return 0; /* ready to go */
}
if (me->valid_label) {
rc = ndmca_media_check_label (sess, 'm', me->label);
if (rc) {
if (rc == -1) {
me->label_io_error = 1;
} else if (rc == -2) {
me->label_read = 1;
me->label_mismatch = 1;
}
goto close_and_unload;
}
me->label_read = 1;
rc = ndmca_media_mtio_tape (sess, NDMP9_MTIO_REW, 1, 0);
if (rc) {
me->media_io_error = 1;
}
}
if (!me->valid_filemark) { /* synthetic */
me->valid_filemark = 1;
if (me->valid_label)
me->file_mark_offset = 1;
else
me->file_mark_offset = 0;
}
count = me->file_mark_offset;
if (count > 0) {
rc = ndmca_media_mtio_tape (sess, NDMP9_MTIO_FSF, count, 0);
if (rc) {
me->fmark_error = 1;
ndmca_media_mtio_tape (sess, NDMP9_MTIO_REW, 1, 0);
goto close_and_unload;
}
}
if (ca->tape_mode == NDMP9_TAPE_RDWR_MODE)
me->media_written = 1; /* most likely */
return 0;
close_and_unload:
me->media_io_error = 1;
ndmca_media_unload_best_effort (sess);
return rc;
}
int
ndmca_media_unload_current (struct ndm_session *sess)
{
struct ndm_control_agent *ca = sess->control_acb;
struct ndm_job_param * job = &ca->job;
struct ndmmedia * me;
int rc;
if (!ca->media_is_loaded)
return 0;
rc = ndmca_media_mtio_tape (sess, NDMP9_MTIO_REW, 1, 0);
if (rc) return rc;
if (ca->job.use_eject) {
rc = ndmca_media_mtio_tape (sess, NDMP9_MTIO_OFF, 1, 0);
if (rc) return rc;
}
rc = ndmca_media_close_tape (sess);
if (rc) return rc;
for (me = job->media_tab.head; me; me = me->next) {
if (me->index == ca->cur_media_ix)
break;
}
if (me && job->have_robot) {
rc = ndmca_robot_unload (sess, me->slot_addr);
if (rc) return rc;
}
ca->media_is_loaded = 0;
return 0;
}
int
ndmca_media_unload_best_effort (struct ndm_session *sess)
{
struct ndm_control_agent *ca = sess->control_acb;
struct ndm_job_param * job = &ca->job;
struct ndmmedia * me;
int errors = 0;
int rc;
if (!ca->media_is_loaded)
return 0;
rc = ndmca_media_mtio_tape (sess, NDMP9_MTIO_REW, 1, 0);
if (rc) errors++;
if (ca->job.use_eject) {
rc = ndmca_media_mtio_tape (sess, NDMP9_MTIO_OFF, 1, 0);
if (rc) errors++;
}
rc = ndmca_media_close_tape (sess);
if (rc) errors++;
if (job->have_robot) {
for (me = job->media_tab.head; me; me = me->next) {
if (me->index == ca->cur_media_ix)
break;
}
if (me) {
rc = ndmca_robot_unload (sess, me->slot_addr);
if (rc) errors++;
} else {
errors++;
}
}
ca->media_is_loaded = 0;
return errors ? -1 : 0;
}
int
ndmca_media_open_tape (struct ndm_session *sess)
{
struct ndm_control_agent *ca = sess->control_acb;
int rc;
unsigned int t;
ndmalogf (sess, 0, 1, "Opening tape drive %s %s",
ca->job.tape_device,
(ca->tape_mode == NDMP9_TAPE_RDWR_MODE)
? "read/write" : "read-only");
rc = -1;
for (t = 0; t <= ca->job.tape_timeout; t += 10) {
if (t > 0) {
ndmalogf (sess, 0, 1,
"Pausing ten seconds before retry (%d/%d)",
t, ca->job.tape_timeout);
sleep (10);
}
rc = ndmca_tape_open(sess);
if (rc == 0) break;
}
if (rc) {
/* should interpret the error */
ndmalogf (sess, 0, 0, "failed open tape drive %s %s",
ca->job.tape_device,
(ca->tape_mode == NDMP9_TAPE_RDWR_MODE)
? "read/write" : "read-only");
}
return rc;
}
int
ndmca_media_close_tape (struct ndm_session *sess)
{
struct ndm_control_agent *ca = sess->control_acb;
int rc;
ndmalogf (sess, 0, 2, "Closing tape drive %s", ca->job.tape_device);
rc = ndmca_tape_close (sess);
return 0;
}
int
ndmca_media_mtio_tape (struct ndm_session *sess,
ndmp9_tape_mtio_op op, u_long count, u_long *resid)
{
int rc;
if (op == NDMP9_MTIO_REW) {
ndmalogf (sess, 0, 1, "Commanding tape drive to rewind");
} else if (op == NDMP9_MTIO_OFF) {
ndmalogf (sess, 0, 1,
"Commanding tape drive to eject (go offline)");
} else {
ndmalogf (sess, 0, 2, "Commanding tape drive to %s %d times",
ndmp9_tape_mtio_op_to_str (op),
count);
}
rc = ndmca_tape_mtio (sess, op, count, resid);
return rc;
}
int
ndmca_media_write_filemarks (struct ndm_session *sess)
{
int rc;
rc = ndmca_media_mtio_tape (sess, NDMP9_MTIO_EOF, 2, 0);
return rc;
}
/*
* Returns: 'm' for tape (media) label
* 'V' for volume label
* '?' for unknown content
* -1 for some kind of error
*/
int
ndmca_media_read_label (struct ndm_session *sess, char labbuf[])
{
char tape_read_buf[512];
int rc;
char * p;
char * q;
ndmalogf (sess, 0, 2, "Reading label");
*labbuf = 0;
rc = ndmca_tape_read (sess, tape_read_buf, 512);
if (rc == 0) {
p = tape_read_buf;
if (strncmp (p, "##ndmjob -m ", 12) == 0) {
p += 12;
rc = 'm';
} else if (strncmp (p, "##ndmjob -V ", 12) == 0) {
p += 12;
rc = 'V';
} else {
rc = '?';
p = 0;
}
if (p) {
q = labbuf;
while (*p && *p != '\n'
&& q < &labbuf[NDMMEDIA_LABEL_MAX-1]) {
*q++ = *p++;
}
*q = 0;
}
} else {
rc = -1;
}
return rc;
}
/*
* type is either 'm' or 'V', see above
*/
int
ndmca_media_write_label (struct ndm_session *sess, int type, char labbuf[])
{
int rc;
char buf[512];
char * p;
ndmalogf (sess, 0, 1, "Writing tape label '%s' type=%c", labbuf, type);
for (p = buf; p < &buf[512]; p++) *p = '#';
for (p = buf+63; p < &buf[512]; p += 64) *p = '\n';
snprintf (buf, sizeof(buf), "##ndmjob -%c %s", type, labbuf);
for (p = buf; *p; p++) continue;
*p++ = '\n';
rc = ndmca_tape_write (sess, buf, 512);
return rc;
}
/*
* type is either 'm' or 'V', see above
*/
int
ndmca_media_check_label (struct ndm_session *sess, int type, char labbuf[])
{
int rc;
char mylabbuf[NDMMEDIA_LABEL_MAX];
ndmalogf (sess, 0, 1, "Checking tape label, expect '%s'", labbuf);
rc = ndmca_media_read_label (sess, mylabbuf);
if (rc < 0) {
ndmalogf (sess, 0, 0, "Label read error");
return -1;
}
if (rc != type || strcmp (labbuf, mylabbuf) != 0) {
ndmalogf (sess, 0, 0,
"Label mismatch, expected -%c'%s', got -%c'%s'",
type, labbuf, rc, mylabbuf);
return -2;
}
return 0;
}
int
ndmca_media_verify (struct ndm_session *sess)
{
struct ndm_control_agent *ca = sess->control_acb;
struct ndm_job_param * job = &ca->job;
int rc;
if (job->have_robot)
return 0; /* not much we can do in advance */
rc = ndmca_robot_verify_media (sess);
if (rc == 0)
return rc;
ndmca_media_tattle (sess);
return -1;
}
int
ndmca_media_tattle (struct ndm_session *sess)
{
struct ndm_control_agent *ca = sess->control_acb;
struct ndm_job_param * job = &ca->job;
struct ndmmedia * me;
int line, nline;
if (!sess->dump_media_info) {
return 0;
}
for (me = job->media_tab.head; me; me = me->next) {
char buf[80];
nline = ndmmedia_pp (me, 0, buf);
ndmalogf (sess, 0, 1, "media #%d %s", me->index, buf);
for (line = 1; line < nline; line++) {
nline = ndmmedia_pp (me, line, buf);
ndmalogf (sess, 0, 2, " %s", buf);
}
}
return 0;
}
/*
* Determine the current byte offset in the current
* tape file.
*/
unsigned long long
ndmca_media_capture_tape_offset (struct ndm_session *sess)
{
struct ndm_control_agent *ca = sess->control_acb;
struct ndm_job_param * job = &ca->job;
int rc;
unsigned long long off;
rc = ndmca_tape_get_state(sess);
if (rc) return NDMP_LENGTH_INFINITY; /* invalid? */
if (!ca->tape_state.blockno.valid)
return NDMP_LENGTH_INFINITY; /* invalid? */
off = ca->tape_state.blockno.value;
off *= job->record_size;
return off;
}
int
ndmca_media_capture_mover_window (struct ndm_session *sess)
{
struct ndm_control_agent *ca = sess->control_acb;
struct ndmlog * ixlog = &ca->job.index_log;
struct ndm_job_param * job = &ca->job;
struct ndmmedia * me;
ndmp9_mover_state ms = ca->mover_state.state;
ndmp9_mover_pause_reason pr = ca->mover_state.pause_reason;
char buf[100];
unsigned long long wlen;
for (me = job->media_tab.head; me; me = me->next) {
if (me->index == ca->cur_media_ix)
break;
}
if (!me) {
return -1;
}
if (ms == NDMP9_MOVER_STATE_PAUSED) {
if (pr == NDMP9_MOVER_PAUSE_SEEK) {
/* end-of-window */
} else if (pr == NDMP9_MOVER_PAUSE_EOM) {
me->media_eom = 1; /* tape full */
} else if (pr == NDMP9_MOVER_PAUSE_EOF) {
me->media_eof = 1;
} else if (pr == NDMP9_MOVER_PAUSE_MEDIA_ERROR) {
me->media_io_error = 1;
} else {
/* what */
}
} else if (ms == NDMP9_MOVER_STATE_HALTED) {
/* if tape_mode == READ, this may not actually be the window */
/* TODO: should STATE_LISTEN be considered? */
} else {
ndmalogf (sess, 0, 1,
"Warning: capturing offset w/o quiescent mover");
}
wlen = ca->mover_state.record_num;
wlen *= job->record_size;
wlen -= job->last_w_offset; /* want the size of this image */
me->valid_n_bytes = 1;
me->nb_determined = 1;
me->n_bytes = wlen;
/*
* Only print the data when a deliver function was defined.
*/
if (ixlog->deliver) {
ndmmedia_pp (me, 0, buf);
ndmlogf (ixlog, "CM", 0, "%02d %s", ca->cur_media_ix, buf);
}
return 0;
}
int
ndmca_media_calculate_windows (struct ndm_session *sess)
{
return 0;
}
int
ndmca_media_calculate_offsets (struct ndm_session *sess)
{
struct ndm_control_agent *ca = sess->control_acb;
struct ndm_job_param * job = &ca->job;
struct ndmmedia * me;
unsigned long long offset = 0;
for (me = job->media_tab.head; me; me = me->next) {
me->begin_offset = offset;
if (me->valid_n_bytes) {
offset += me->n_bytes;
me->end_offset = offset;
} else {
me->n_bytes = NDMP_LENGTH_INFINITY;
me->end_offset = NDMP_LENGTH_INFINITY;
/* offset unchanged */
}
}
return 0;
}
int
ndmca_media_set_window_current (struct ndm_session *sess)
{
struct ndm_control_agent *ca = sess->control_acb;
struct ndm_job_param * job = &ca->job;
struct ndmmedia * me;
int rc;
for (me = job->media_tab.head; me; me = me->next) {
if (me->index == ca->cur_media_ix)
break;
}
if (!me) {
return -1;
}
rc = ndmca_mover_set_window (sess, me->begin_offset, me->n_bytes);
if (rc == 0)
job->last_w_offset = me->begin_offset;
return rc;
}
#endif /* !NDMOS_OPTION_NO_CONTROL_AGENT */