00001
00002
00003
00004
00005
00006 #include <SUM.h>
00007 #include <tape.h>
00008 #include <sum_rpc.h>
00009
00010 extern int write_log(const char *fmt, ...);
00011 extern SLOT slots[];
00012 extern DRIVE drives[];
00013 extern TQ *q_rd_front;
00014 extern TQ *q_rd_rear;
00015 extern TQ *q_wrt_front;
00016 extern TQ *q_wrt_rear;
00017 extern TQ *q_rd_need_front;
00018 extern TQ *q_rd_need_rear;
00019 extern TQ *q_wrt_need_front;
00020 extern TQ *q_wrt_need_rear;
00021
00022
00023
00024
00025
00026
00027
00028
00029 int slot_to_drive(int sim, int slot, int drive)
00030 {
00031 char cmd[128];
00032
00033 sprintf(cmd, "mtx -f %s load %d %d", LIBDEV, slot+1, drive);
00034 write_log("Executing: %s\n", cmd);
00035 if(sim) {
00036 sleep(2);
00037 }
00038 else {
00039 if(system(cmd)) {
00040 write_log(" cmd failure\n");
00041 return(-1);
00042 }
00043 }
00044 write_log(" cmd success\n");
00045 drives[drive].tapeid = slots[slot].tapeid;
00046 drives[drive].sumid = 0;
00047 drives[drive].busy = 0;
00048 drives[drive].tapemode = 0;
00049 drives[drive].filenum = 0;
00050 drives[drive].blocknum = 0;
00051 drives[drive].slotnum = slot;
00052 slots[slot].tapeid = NULL;
00053 return(slot);
00054 }
00055
00056
00057
00058
00059
00060 int drive_to_slot(int sim, int drive, int slot)
00061 {
00062 char cmd[128];
00063
00064 sprintf(cmd, "mtx -f %s unload %d %d", LIBDEV, slot, drive);
00065 write_log("Executing: %s\n", cmd);
00066 if(sim) {
00067 sleep(2);
00068 }
00069 else {
00070 if(system(cmd)) {
00071 write_log(" cmd failure\n");
00072 return(-1);
00073 }
00074 }
00075 write_log(" cmd success\n");
00076 slots[slot].tapeid = drives[drive].tapeid;
00077 drives[drive].tapeid = NULL;
00078 drives[drive].sumid = 0;
00079 drives[drive].busy = 0;
00080 drives[drive].tapemode = 0;
00081 drives[drive].filenum = 0;
00082 drives[drive].blocknum = 0;
00083 drives[drive].slotnum = -1;
00084 return(drive);
00085 }
00086
00087
00088
00089
00090 int slot_to_slot(int sim, int slota, int slotb)
00091 {
00092 char cmd[128];
00093
00094 sprintf(cmd, "mtx -f %s transfer %d %d", LIBDEV, slota+1, slotb+1);
00095 write_log("Executing: %s\n", cmd);
00096 if(sim) {
00097 sleep(2);
00098 }
00099 else {
00100 if(system(cmd)) {
00101 write_log(" cmd failure\n");
00102 return(-1);
00103 }
00104 }
00105 write_log(" cmd success\n");
00106 slots[slotb].tapeid = slots[slota].tapeid;
00107 slots[slota].tapeid = NULL;
00108 return(slota);
00109 }
00110
00111
00112
00113 int find_empty_impexp_slot()
00114 {
00115 int i;
00116
00117 for(i=(MAX_SLOTS - NUM_IMP_EXP_SLOTS); i < MAX_SLOTS; i++) {
00118 if(slots[i].tapeid == NULL) {
00119 return(i);
00120 }
00121 }
00122 return(-1);
00123 }
00124
00125
00126
00127
00128 int tapeindrive(char *tape)
00129 {
00130 int i;
00131
00132 for(i=0; i < MAX_DRIVES; i++) {
00133 if(!drives[i].tapeid) continue;
00134 if(!strcmp(tape, drives[i].tapeid)) {
00135 return(i);
00136 }
00137 }
00138 return(-1);
00139 }
00140
00141
00142
00143
00144 int tapeinslot(char *tape)
00145 {
00146 int i;
00147
00148 for(i=0; i < MAX_SLOTS_LIVE; i++) {
00149 if(!slots[i].tapeid) continue;
00150 if(!strcmp(tape, slots[i].tapeid)) {
00151 return(i);
00152 }
00153 }
00154 return(-1);
00155 }
00156
00157
00158
00159 int findfreedrive()
00160 {
00161 int i;
00162
00163 for(i=0; i < MAX_DRIVES; i++) {
00164 if(drives[i].tapeid == NULL) {
00165 return(i);
00166 }
00167 }
00168 return(-1);
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 int tape_free_drives()
00195 {
00196 int d, s;
00197 int nobar = 0;
00198 char cmd[128];
00199
00200 for(d=0; d < MAX_DRIVES; d++) {
00201
00202 if(!drives[d].tapeid) continue;
00203 #ifdef SUMDC
00204
00205 if(strstr(drives[d].tapeid, "NoBar")) { nobar = 1; }
00206
00207 if(strstr(drives[d].tapeid, "CLN")) { nobar = 1; }
00208 #endif
00209
00210 for(s=0; s < MAX_SLOTS_LIVE; s++) {
00211 if(nobar) {
00212 s = MAX_SLOTS_LIVE - 1;
00213 }
00214 if(!slots[s].tapeid) {
00215 sprintf(cmd, "%s %d %d 1> %s 2>&1", UNLOADCMD, s+1, d, UNLOADDUMP);
00216 write_log("*Rb:cmd: %s\n", cmd);
00217 if(system(cmd)) {
00218 write_log("Err Retry: %s\n", cmd);
00219 if(system(cmd)) {
00220 write_log("***Rb:failure\n\n");
00221 return(0);
00222 }
00223 }
00224 write_log("***Rb:success\n\n");
00225 slots[s].tapeid = drives[d].tapeid;
00226 slots[s].slotnum = s;
00227 drives[d].tapeid = NULL;
00228 drives[d].sumid = 0;
00229 drives[d].busy = 0;
00230 drives[d].tapemode = 0;
00231 drives[d].filenum = 0;
00232 drives[d].blocknum = 0;
00233 drives[d].slotnum = -1;
00234 break;
00235 }
00236 }
00237 if(s == MAX_SLOTS_LIVE) {
00238 if(nobar)
00239 write_log("Must have slot# %d free for cleaning tape\n", MAX_SLOTS_LIVE);
00240 else
00241 write_log("No free slots to unload drive\n");
00242 return(0);
00243 }
00244 }
00245 return(1);
00246 }
00247
00248
00249
00250 int empty_q_rd(void) {
00251 return q_rd_rear == NULL;
00252 }
00253
00254 int empty_q_wrt(void) {
00255 return q_wrt_rear == NULL;
00256 }
00257
00258 int empty_q_rd_need(void) {
00259 return q_rd_need_rear == NULL;
00260 }
00261
00262 int empty_q_wrt_need(void) {
00263 return q_wrt_need_rear == NULL;
00264 }
00265
00266
00267 TQ *q_entry_make(KEY *list, SUMID_t uid, char *tapeid, int filenum,
00268 char *user, uint64_t dsix) {
00269 CLIENT *client;
00270 TQ *p = (TQ *)malloc(sizeof(TQ));
00271 if(p != NULL) {
00272
00273 p->next = NULL;
00274 p->uid = uid;
00275 p->ds_index = dsix;
00276 p->tapeid = strdup(tapeid);
00277 p->username = strdup(user);
00278 p->filenum = filenum;
00279 p->list = newkeylist();
00280 add_keys(list, &p->list);
00281
00282 if(client = (CLIENT *)getkey_fileptr(list, "current_client")) {
00283 setkey_fileptr(&p->list, "current_client", (FILE *)client);
00284 }
00285 }
00286 return p;
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296 void tq_entry_rd_dump(char *user) {
00297 TQ *q = q_rd_front;
00298 while(q) {
00299 write_log("*Tp:RdQadd: uid=%lu tapeid=%s filenum=%d user=%s dsix=%lu\n",
00300 q->uid, q->tapeid, q->filenum, user, q->ds_index);
00301 q = q->next;
00302 }
00303 }
00304 void tq_entry_rd_dump2() {
00305 TQ *q = q_rd_front;
00306 while(q) {
00307 write_log("*Tp:RdQadd: uid=%lu tapeid=%s filenum=%d user=%s dsix=%lu\n",
00308 q->uid, q->tapeid, q->filenum, q->username, q->ds_index);
00309 q = q->next;
00310 }
00311 }
00312
00313
00314
00315
00316
00317 void insert_tq_entry_rd_sort(TQ *p) {
00318 TQ *qprev = q_rd_front;
00319 TQ *q = q_rd_front;
00320 if(q == NULL) {
00321 insert_tq_entry_rd(p);
00322 return;
00323 }
00324 while(q) {
00325 if(q->filenum > p->filenum) {
00326 if(q_rd_front == q) {
00327 q_rd_front = p;
00328 p->next = q;
00329 }
00330 else {
00331 qprev->next = p;
00332 p->next = q;
00333 }
00334 return;
00335 }
00336 else {
00337 if(q->next == NULL) {
00338 insert_tq_entry_rd(p);
00339 return;
00340 }
00341 }
00342 qprev = q;
00343 q = q->next;
00344 }
00345 }
00346
00347
00348 void insert_tq_entry_rd(TQ *p) {
00349 if(empty_q_rd()) q_rd_front = p;
00350 else q_rd_rear->next = p;
00351 q_rd_rear = p;
00352 }
00353
00354 void insert_tq_entry_wrt(TQ *p) {
00355 if(empty_q_wrt()) q_wrt_front = p;
00356 else q_wrt_rear->next = p;
00357 q_wrt_rear = p;
00358 }
00359
00360 void insert_tq_entry_rd_need(TQ *p) {
00361 TQ *px;
00362
00363 if(empty_q_rd_need()) {
00364 q_rd_need_front = p;
00365 q_rd_need_rear = p;
00366 return;
00367 }
00368 px = q_rd_need_front;
00369 while(px) {
00370 if((px->uid == p->uid) && (px->filenum == p->filenum) && (!strcmp(px->tapeid, p->tapeid))) {
00371 return;
00372 }
00373 px = px->next;
00374 }
00375 q_rd_need_rear->next = p;
00376 q_rd_need_rear = p;
00377 }
00378
00379 void insert_tq_entry_wrt_need(TQ *p) {
00380 if(empty_q_wrt_need()) q_wrt_need_front = p;
00381 else q_wrt_need_rear->next = p;
00382 q_wrt_need_rear = p;
00383 }
00384
00385 void rd_q_print(TQ *p) {
00386 while(p) {
00387
00388
00389 write_log("uid=%lu, dsix=%lu, filenum=%d, tapeid=%s, user=%s\n",
00390 p->uid, p->ds_index, p->filenum, p->tapeid, p->username);
00391 p = p->next;
00392 }
00393 }
00394
00395
00396 TQ *delete_q_rd_front(void) {
00397 TQ *p = q_rd_front;
00398 if(q_rd_front == q_rd_rear) q_rd_rear = q_rd_front = NULL;
00399 else q_rd_front = p->next;
00400 return(p);
00401 }
00402
00403
00404 TQ *delete_q_wrt_front(void) {
00405 TQ *p = q_wrt_front;
00406 if(q_wrt_front == q_wrt_rear) q_wrt_rear = q_wrt_front = NULL;
00407 else q_wrt_front = p->next;
00408 return(p);
00409 }
00410
00411
00412 TQ *delete_q_rd_need_front(void) {
00413 TQ *p = q_rd_need_front;
00414 if(q_rd_need_front == q_rd_need_rear) q_rd_need_rear = q_rd_need_front = NULL;
00415 else q_rd_need_front = p->next;
00416 return(p);
00417 }
00418
00419
00420 TQ *delete_q_wrt_need_front(void) {
00421 TQ *p = q_wrt_need_front;
00422 if(q_wrt_need_front == q_wrt_need_rear) q_wrt_need_rear = q_wrt_need_front = NULL;
00423 else q_wrt_need_front = p->next;
00424 return(p);
00425 }
00426
00427
00428
00429
00430 TQ *delete_q_rd(TQ *p) {
00431 TQ *e = NULL;
00432 TQ *f = NULL;
00433 if(p == q_rd_front) {
00434 q_rd_front = p->next;
00435 if(q_rd_front == NULL) q_rd_rear = NULL;
00436 return(p);
00437 }
00438 e = q_rd_front->next;
00439 f = q_rd_front;
00440 while(e != NULL) {
00441 if(e == p) {
00442 f->next = e->next;
00443 if(p == q_rd_rear) q_rd_rear = f;
00444 return(p);
00445 }
00446 f = e;
00447 e = e->next;
00448 }
00449 return(e);
00450 }
00451
00452
00453
00454
00455 TQ *delete_q_wrt(TQ *p) {
00456 TQ *e, *f;
00457 if(p == q_wrt_front) {
00458 q_wrt_front = p->next;
00459 if(q_wrt_front == NULL) q_wrt_rear = NULL;
00460 return(p);
00461 }
00462 e = q_wrt_front->next;
00463 f = q_wrt_front;
00464 while(e != NULL) {
00465 if(e == p) {
00466 f->next = e->next;
00467 if(p == q_wrt_rear) q_wrt_rear = f;
00468 return(p);
00469 }
00470 f = e;
00471 e = e->next;
00472 }
00473 return(e);
00474 }
00475
00476
00477
00478
00479 TQ *delete_q_rd_need(TQ *p) {
00480 TQ *e = NULL;
00481 TQ *f = NULL;
00482 if(p == q_rd_need_front) {
00483 q_rd_need_front = p->next;
00484 if(q_rd_need_front == NULL) q_rd_need_rear = NULL;
00485 return(p);
00486 }
00487 e = q_rd_need_front->next;
00488 f = q_rd_need_front;
00489 while(e != NULL) {
00490 if(e == p) {
00491 f->next = e->next;
00492 if(p == q_rd_need_rear) q_rd_need_rear = f;
00493 return(p);
00494 }
00495 f = e;
00496 e = e->next;
00497 }
00498 return(e);
00499 }
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521 SUMOFFCNT *setsumoffcnt(SUMOFFCNT **list, SUMID_t uid, int offcnt)
00522 {
00523 SUMOFFCNT *walk;
00524 SUMOFFCNT *newone;
00525
00526 walk = *list;
00527 while(walk) {
00528 if(walk->uid != uid)
00529 walk = walk->next;
00530 else
00531 return(NULL);
00532 }
00533 newone = (SUMOFFCNT *)malloc(sizeof(SUMOFFCNT));
00534 if(newone == NULL) return(newone);
00535 newone->next = *list;
00536 newone->uid = uid;
00537 newone->offcnt = offcnt;
00538 newone->uniqcnt = 0;
00539 *list = newone;
00540 return(newone);
00541 }
00542
00543 SUMOFFCNT *getsumoffcnt(SUMOFFCNT *list, SUMID_t uid)
00544 {
00545 SUMOFFCNT *walk = list;
00546
00547 while(walk) {
00548 if(walk->uid != uid)
00549 walk = walk->next;
00550 else
00551 return walk;
00552 }
00553 return walk;
00554 }
00555
00556 void remsumoffcnt(SUMOFFCNT **list, SUMID_t uid)
00557 {
00558 SUMOFFCNT *walk = *list;
00559 SUMOFFCNT *trail = NULL;
00560 int i;
00561
00562 while(walk) {
00563 if(walk->uid != uid) {
00564 trail = walk;
00565 walk = walk->next;
00566 }
00567 else {
00568 if(trail)
00569 trail->next = walk->next;
00570 else
00571 *list = walk->next;
00572 for(i = 0; i < walk->uniqcnt; i++) {
00573 free(walk->tapeids[i]);
00574 }
00575 free(walk);
00576 walk = NULL;
00577 }
00578 }
00579 }