File: ndma_ctrl_robot.c

package info (click to toggle)
amanda 1%3A3.3.9-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 26,440 kB
  • sloc: ansic: 227,845; perl: 61,130; xml: 14,619; sh: 10,221; makefile: 2,364; awk: 431; lex: 405; yacc: 342; tcl: 118; sql: 19; sed: 16; php: 2
file content (496 lines) | stat: -rw-r--r-- 11,070 bytes parent folder | download | duplicates (3)
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
/*
 * 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_robot_issue_scsi_req (struct smc_ctrl_block *smc)
{
	struct ndmconn *	conn = (struct ndmconn *) smc->app_data;
	struct smc_scsi_req *	sr = &smc->scsi_req;
	int			rc;

	rc = ndmscsi_execute (conn, (struct ndmscsi_request *) sr, 0);
	return rc;
}


int
ndmca_robot_prep_target (struct ndm_session *sess)
{
	struct smc_ctrl_block *	smc = &sess->control_acb.smc_cb;
	int			rc;

	NDMOS_MACRO_ZEROFILL (smc);

	smc->app_data = sess->plumb.robot;
	smc->issue_scsi_req = ndmca_robot_issue_scsi_req;

	rc = ndmscsi_use (sess->plumb.robot,
				&sess->control_acb.job.robot_target);
	if (rc) return rc;

	return 0;
}

int
ndmca_robot_obtain_info (struct ndm_session *sess)
{
	struct smc_ctrl_block *	smc = &sess->control_acb.smc_cb;
	int			rc;

	rc = smc_inquire (smc);
	if (rc) return rc;

	rc = smc_get_elem_aa (smc);
	if (rc) return rc;

	rc = smc_read_elem_status (smc);
	if (rc) return rc;

	return 0;
}

int
ndmca_robot_init_elem_status (struct ndm_session *sess)
{
	struct smc_ctrl_block *	smc = &sess->control_acb.smc_cb;
	int			rc;

	ndmalogf (sess, 0, 1,
	    "Commanding robot to initialize element status (take inventory)");

	rc = smc_init_elem_status (smc);
	if (rc) {
		ndmalogf (sess, 0, 0, "init-elem-status failed");
		return rc;
	}

	return 0;
}

int
ndmca_robot_startup (struct ndm_session *sess)
{
	int		rc;

	if (!sess->control_acb.job.have_robot)
		return -1;	/* Huh? why were we called */

	rc = ndmca_connect_robot_agent(sess);
	if (rc) return rc;

	rc = ndmca_robot_prep_target(sess);
	if (rc) return rc;

	return 0;
}

int
ndmca_robot_move (struct ndm_session *sess, int src_addr, int dst_addr)
{
	struct ndm_control_agent *ca = &sess->control_acb;
	struct smc_ctrl_block *	smc = &ca->smc_cb;
	int			rc;
	unsigned int		t;

	ndmalogf (sess, 0, 2, "robot moving @%d to @%d",
			src_addr, dst_addr);

	rc = -1;
	for (t = 0; t <= ca->job.robot_timeout; t += 10) {
		if (t > 0) {
			ndmalogf (sess, 0, 2,
				"Pausing ten seconds before retry (%d/%d)",
				t, ca->job.robot_timeout);
			sleep (10);
		}
		rc = smc_move (smc, src_addr, dst_addr,
					0, smc->elem_aa.mte_addr);
		if (rc == 0) break;
	}

	if (rc == 0) {
		ndmalogf (sess, 0, 2, "robot move OK @%d to @%d",
				src_addr, dst_addr);
	} else {
		ndmalogf (sess, 0, 2, "robot move BAD @%d to @%d",
			src_addr, dst_addr);
	}

	return rc;
}

int
ndmca_robot_load (struct ndm_session *sess, int slot_addr)
{
	struct smc_ctrl_block *	smc = &sess->control_acb.smc_cb;
	unsigned		dte_addr = smc->elem_aa.dte_addr;
	int			rc;

	if (sess->control_acb.job.drive_addr_given)
		dte_addr = sess->control_acb.job.drive_addr;

	ndmalogf (sess, 0, 1,
			"Commanding robot to load slot @%d into drive @%d",
			slot_addr, dte_addr);

	rc = ndmca_robot_move (sess, slot_addr, dte_addr);

	return rc;
}

int
ndmca_robot_unload (struct ndm_session *sess, int slot_addr)
{
	struct smc_ctrl_block *	smc = &sess->control_acb.smc_cb;
	int			dte_addr = smc->elem_aa.dte_addr;
	int			rc;

	if (sess->control_acb.job.drive_addr_given)
		dte_addr = sess->control_acb.job.drive_addr;

	/* tricky part -- some (most?) robots need the drive to eject */

	ndmalogf (sess, 0, 1,
			"Commanding robot to unload drive @%d to slot @%d",
			dte_addr, slot_addr);

	rc = ndmca_robot_move (sess, dte_addr, slot_addr);

	return rc;
}


struct smc_element_descriptor *
ndmca_robot_find_element (struct ndm_session *sess, int element_address)
{
	struct smc_ctrl_block *		smc = &sess->control_acb.smc_cb;
	unsigned int			i;
	struct smc_element_descriptor *	edp;

	for (i = 0; i < smc->n_elem_desc; i++) {
		edp = &smc->elem_desc[i];
		if (edp->element_address == element_address)
			return edp;
	}

	return 0;
}

int
ndmca_robot_check_ready (struct ndm_session *sess)
{
	struct smc_ctrl_block *		smc = &sess->control_acb.smc_cb;
	unsigned			first_dte_addr;
	unsigned			n_dte_addr;
	int				rc;
	unsigned int			i;
	int				errcnt = 0;
	struct smc_element_descriptor *	edp;

	rc = ndmca_robot_obtain_info (sess);
	if (rc) return rc;

	if (sess->control_acb.job.remedy_all) {
		first_dte_addr = smc->elem_aa.dte_addr;
		n_dte_addr = smc->elem_aa.dte_count;
	} else {
		n_dte_addr = 1;
		if (sess->control_acb.job.drive_addr_given) {
			first_dte_addr = sess->control_acb.job.drive_addr;
		} else {
			first_dte_addr = smc->elem_aa.dte_addr;
		}
	}

	for (i = 0; i < n_dte_addr; i++) {
		edp = ndmca_robot_find_element (sess, first_dte_addr+i);

		if (!edp->Full)
			continue;

		ndmalogf (sess, 0, 1, "tape drive @%d not empty",
				edp->element_address);
		errcnt++;
	}

	return errcnt;
}

int
ndmca_robot_remedy_ready (struct ndm_session *sess)
{
	struct smc_ctrl_block *		smc = &sess->control_acb.smc_cb;
	int				rc;
	unsigned int			i;
	int				errcnt;
	struct smc_element_descriptor *	edp;
	struct smc_element_descriptor *	edp2;
	unsigned			first_dte_addr;
	unsigned			n_dte_addr;
	char				prefix[60];

	errcnt = 0;

	rc = ndmca_robot_obtain_info (sess);
	if (rc) return rc;

	if (sess->control_acb.job.remedy_all) {
		first_dte_addr = smc->elem_aa.dte_addr;
		n_dte_addr = smc->elem_aa.dte_count;
	} else {
		n_dte_addr = 1;
		if (sess->control_acb.job.drive_addr_given) {
			first_dte_addr = sess->control_acb.job.drive_addr;
		} else {
			first_dte_addr = smc->elem_aa.dte_addr;
		}
	}

	for (i = 0; i < n_dte_addr; i++) {
		edp = ndmca_robot_find_element (sess, first_dte_addr+i);

		if (!edp->Full)
			continue;

		sprintf (prefix, "drive @%d not empty", edp->element_address);

		if (!edp->SValid) {
			ndmalogf (sess, 0, 1, "%s, invalid source", prefix);
			errcnt++;
			continue;
		}

		sprintf (NDMOS_API_STREND(prefix), ", src @%d",
							edp->src_se_addr);

		edp2 = ndmca_robot_find_element (sess, edp->src_se_addr);

		if (edp2->element_type_code != SMC_ELEM_TYPE_SE) {
			ndmalogf (sess, 0, 1, "%s, not slot", prefix);
			errcnt++;
			continue;
		}

		if (edp2->Full) {
			ndmalogf (sess, 0, 1, "%s, but slot Full", prefix);
			errcnt++;
			continue;
		}

		rc = ndmca_robot_move (sess,
				edp->element_address, edp->src_se_addr);
		if (rc) {
			ndmalogf (sess, 0, 1, "%s, move failed", prefix);
			errcnt++;
			continue;
		}
	}

	return errcnt;
}



/*
 * ndmca_robot_query() incrementally obtains info so that we
 * can print progress.
 */

int
ndmca_robot_query (struct ndm_session *sess)
{
	struct smc_ctrl_block *	smc = &sess->control_acb.smc_cb;
	int			rc;
	unsigned int		i;
	char			buf[100];
	char			lnbuf[30];
	int			lineno, nline = 1;

	ndmalogqr (sess, "  Type");

	rc = smc_inquire (smc);
	if (rc) {
		ndmalogqr (sess, "    ERROR smc_inquire(): %s", smc->errmsg);
	} else {
		ndmalogqr (sess, "    '%s'", smc->ident);
	}


	ndmalogqr (sess, "  Elements");
	rc = smc_get_elem_aa (smc);
	if (rc) {
		ndmalogqr (sess, "    ERROR smc_get_elem_aa(): %s", smc->errmsg);
	} else {
		strcpy (lnbuf, "    ");
		for (lineno = 0, nline = 1; lineno < nline; lineno++) {
			rc = smc_pp_element_address_assignments (&smc->elem_aa,
								lineno, buf);
			if (rc < 0) {
				strcpy (buf, "PP-ERROR");
			}
			nline = rc;
			ndmalogqr (sess, "%s %s", lnbuf, buf);
		}
	}

	ndmalogqr (sess, "  Status");
	rc = smc_read_elem_status (smc);
	if (rc) {
		ndmalogqr (sess, "    ERROR smc_read_elem_status(): %s", smc->errmsg);
	} else {
		ndmalogqr (sess, "    E#  Addr Type Status");
		ndmalogqr (sess, "    --  ---- ---- ---------------------");
		for (i = 0; i < smc->n_elem_desc; i++) {
			struct smc_element_descriptor *	edp;

			edp = &smc->elem_desc[i];

			for (lineno = 0, nline = 1; lineno < nline; lineno++) {
				rc = smc_pp_element_descriptor (edp,
								lineno, buf);

				if (lineno == 0)
					sprintf (lnbuf, "    %2d ", i+1);
				else
					sprintf (lnbuf, "       ");

				if (rc < 0) {
					strcpy (buf, "PP-ERROR");
				}
				nline = rc;
				ndmalogqr (sess, "%s %s", lnbuf, buf);
			}
		}
	}

	return 0;
}


int
ndmca_robot_verify_media (struct ndm_session *sess)
{
	struct smc_ctrl_block *	smc = &sess->control_acb.smc_cb;
	struct ndm_media_table *mtab = &sess->control_acb.job.media_tab;
	int			rc;
	struct ndmmedia *	me;
	struct smc_element_descriptor *edp;
	int			i;
	unsigned int		j;
	int			errcnt = 0;

	rc = ndmca_robot_obtain_info (sess);
	if (rc) return rc;

	for (i = 0; i < mtab->n_media; i++) {
		me = &mtab->media[i];

		if (! me->valid_slot) {
			me->slot_missing = 1;
			errcnt++;
			continue;	/* what now */
		}

		for (j = 0; j < smc->n_elem_desc; j++) {
			edp = &smc->elem_desc[j];

			if (edp->element_type_code != SMC_ELEM_TYPE_SE)
				continue;

			if (edp->element_address != me->slot_addr)
				continue;

			if (!edp->Full) {
				me->slot_empty = 1;
				errcnt++;
			} else {
				me->slot_empty = 0;
			}
			break;
		}
		if (j >= smc->n_elem_desc) {
			me->slot_bad = 1;
			errcnt++;
		}
	}

	return errcnt;
}

/*
 * For NDM_JOB_OP_LIST_LABELS, fill in media_tab based on non-empty slots.
 * Note: this might REALLY nerf on a cleaning cartridge.
 */

int
ndmca_robot_synthesize_media (struct ndm_session *sess)
{
	struct smc_ctrl_block *	smc = &sess->control_acb.smc_cb;
	struct ndm_media_table *mtab = &sess->control_acb.job.media_tab;
	int			rc;
	struct ndmmedia *	me;
	struct smc_element_descriptor *edp;
	unsigned int		i;

	rc = ndmca_robot_obtain_info (sess);
	if (rc) return rc;

	for (i = 0; i < smc->n_elem_desc; i++) {
		edp = &smc->elem_desc[i];

		if (edp->element_type_code != SMC_ELEM_TYPE_SE)
			continue;

		if (!edp->Full)
			continue;

		me = &mtab->media[mtab->n_media++];
		NDMOS_MACRO_ZEROFILL (me);

		me->valid_slot = 1;
		me->slot_addr = edp->element_address;
	}

	return 0;
}
#endif /* !NDMOS_OPTION_NO_CONTROL_AGENT */