2 * $Id: chg-scsi-chio.c,v 1.12 2006/07/25 18:18:46 martinea Exp $
4 * chg-scsi-chio.c -- generic SCSI changer driver
6 * This program provides a driver to control generic
7 * SCSI changers, no matter what platform. The host/OS
8 * specific portions of the interface are implemented
9 * in libscsi.a, which contains a module for each host/OS.
10 * The actual interface for HP/UX is in scsi-hpux.c;
11 * chio is in scsi-chio.c, etc.. A prototype system
12 * dependent scsi interface file is in scsi-proto.c.
14 * Copyright 1997, 1998 Eric Schnoebelen <eric@cirr.com>
16 * This module based upon seagate-changer, by Larry Pyeatt
17 * <pyeatt@cs.colostate.edu>
19 * The original introductory comments follow:
21 * This program was written to control the Seagate/Conner/Archive
22 * autoloading DAT drive. This drive normally has 4 tape capacity
23 * but can be expanded to 12 tapes with an optional tape cartridge.
24 * This program may also work on onther drives. Try it and let me
25 * know of successes/failures.
27 * I have attempted to conform to the requirements for Amanda tape
28 * changer interface. There could be some bugs.
30 * This program works for me under Linux with Gerd Knorr's
31 * <kraxel@cs.tu-berlin.de> SCSI media changer driver installed
32 * as a kernel module. The kernel module is available at
33 * http://sunsite.unc.edu/pub/Linux/kernel/patches/scsi/scsi-changer*
34 * Since the Linux media changer is based on NetBSD, this program
35 * should also work for NetBSD, although I have not tried it.
36 * It may be necessary to change the IOCTL calls to work on other
39 * (c) 1897 Larry Pyeatt, pyeatt@cs.colostate.edu
40 * All Rights Reserved.
42 * Permission to use, copy, modify, distribute, and sell this software and its
43 * documentation for any purpose is hereby granted without fee, provided that
44 * the above copyright notice appear in all copies and that both that
45 * copyright notice and this permission notice appear in supporting
46 * documentation. The author makes no representations about the
47 * suitability of this software for any purpose. It is provided "as is"
48 * without express or implied warranty.
50 * Michael C. Povel 03.06.98 added ejetct_tape and sleep for external tape
51 * devices, and changed some code to allow multiple drives to use their
52 * own slots. Also added support for reserverd slots.
53 * At the moment these parameters are hard coded and only tested under Linux
61 #include "scsi-defs.h"
63 char *tapestatfile = NULL;
65 /*----------------------------------------------------------------------------*/
68 NUMDRIVE,EJECT,SLEEP,CLEANMAX,DRIVE,START,END,CLEAN,DEVICE,STATFILE,CLEANFILE,DRIVENUM,
69 CHANGERDEV,USAGECOUNT,SCSITAPEDEV, TAPESTATFILE
77 tokentable_t t_table[]={
78 { "number_configs", NUMDRIVE},
81 { "cleanmax", CLEANMAX},
85 { "cleancart", CLEAN},
87 { "statfile", STATFILE},
88 { "cleanfile", CLEANFILE},
89 { "drivenum", DRIVENUM},
90 { "changerdev", CHANGERDEV},
91 { "usagecount", USAGECOUNT},
92 { "scsitapedev", SCSITAPEDEV},
93 { "tapestatus", TAPESTATFILE},
99 void init_changer_struct(changer_t *chg, size_t number_of_config);
100 void dump_changer_struct(changer_t *chg);
101 void free_changer_struct(changer_t **changer);
102 void parse_line(char *linebuffer,int *token,char **value);
103 int read_config(char *configfile, changer_t *chg);
104 int get_current_slot(char *count_file);
105 void put_current_slot(char *count_file,int slot);
106 void usage(char *argv[]);
107 void parse_args(int argc, char *argv[],command *rval);
108 int get_relative_target(int fd,int nslots,char *parameter,int loaded,
109 char *changer_file,int slot_offset,int maxslot);
110 int is_positive_number(char *tmp);
111 int ask_clean(char *tapedev);
112 void clean_tape(int fd,char *tapedev,char *cnt_file, int drivenum,
113 int cleancart, int maxclean,char *usagetime);
114 int main(int argc, char *argv[]);
118 * Initialize data structures with default values
123 size_t number_of_config)
127 memset(chg, 0, SIZEOF(*chg));
128 chg->number_of_configs = number_of_config;
133 chg->conf = alloc(SIZEOF(config_t) * number_of_config);
134 for (i=0; i < number_of_config; i++){
135 chg->conf[i].drivenum = 0;
136 chg->conf[i].start = -1;
137 chg->conf[i].end = -1;
138 chg->conf[i].cleanslot = -1;
139 chg->conf[i].device = NULL;
140 chg->conf[i].slotfile = NULL;
141 chg->conf[i].cleanfile = NULL;
142 chg->conf[i].timefile = NULL;
143 chg->conf[i].scsitapedev = NULL;
144 chg->conf[i].tapestatfile = NULL;
145 chg->conf[i].changerident = NULL;
146 chg->conf[i].tapeident = NULL;
151 * Dump of information for debug
159 dbprintf(("Number of configurations: %d\n", chg->number_of_configs));
160 dbprintf(("Tapes need eject: %s\n", (chg->eject>0 ? "Yes" : "No")));
161 dbprintf(("Tapes need sleep: %d seconds\n", chg->sleep));
162 dbprintf(("Cleancycles : %d\n", chg->cleanmax));
163 dbprintf(("Changerdevice : %s\n", chg->device));
164 for (i = 0; i < chg->number_of_configs; i++){
165 dbprintf(("Tapeconfig Nr: %d\n", i));
166 dbprintf((" Drivenumber : %d\n", chg->conf[i].drivenum));
167 dbprintf((" Startslot : %d\n", chg->conf[i].start));
168 dbprintf((" Endslot : %d\n", chg->conf[i].end));
169 dbprintf((" Cleanslot : %d\n", chg->conf[i].cleanslot));
170 if (chg->conf[i].device != NULL)
171 dbprintf((" Devicename : %s\n", chg->conf[i].device));
173 dbprintf((" Devicename : none\n"));
174 if (chg->conf[i].scsitapedev != NULL)
175 dbprintf((" SCSITapedev : %s\n", chg->conf[i].scsitapedev));
177 dbprintf((" SCSITapedev : none\n"));
178 if (chg->conf[i].tapestatfile != NULL)
179 dbprintf((" statfile : %s\n", chg->conf[i].tapestatfile));
181 dbprintf((" statfile : none\n"));
182 if (chg->conf[i].slotfile != NULL)
183 dbprintf((" Slotfile : %s\n", chg->conf[i].slotfile));
185 dbprintf((" Slotfile : none\n"));
186 if (chg->conf[i].cleanfile != NULL)
187 dbprintf((" Cleanfile : %s\n", chg->conf[i].cleanfile));
189 dbprintf((" Cleanfile : none\n"));
190 if (chg->conf[i].timefile != NULL)
191 dbprintf((" Usagecount : %s\n", chg->conf[i].timefile));
193 dbprintf((" Usagecount : none\n"));
198 * Free all allocated memory
207 assert(changer != NULL);
208 assert(*changer != NULL);
211 if (chg->device != NULL)
213 for (i=0; i<chg->number_of_configs; i++){
214 if (chg->conf[i].device != NULL)
215 amfree(chg->conf[i].device);
216 if (chg->conf[i].slotfile != NULL)
217 amfree(chg->conf[i].slotfile);
218 if (chg->conf[i].cleanfile != NULL)
219 amfree(chg->conf[i].cleanfile);
220 if (chg->conf[i].timefile != NULL)
221 amfree(chg->conf[i].timefile);
223 if (chg->conf != NULL)
231 * This function parses a line, and returns a token and value
242 *token = -1; /* No Token found */
243 tok=strtok(linebuffer," \t\n");
245 while ((tok != NULL) && (tok[0]!='#')&&(ready==0)){
251 while ((t_table[i].word != NULL)&&(*token==-1)){
252 if (0==strcasecmp(t_table[i].word,tok)){
253 *token=t_table[i].token;
258 tok=strtok(NULL," \t\n");
264 * This function reads the specified configfile and fills the structure
279 numconf = 1; /* At least one configuration is assumed */
280 /* If there are more, it should be the first entry in the configurationfile */
282 if (NULL==(file=fopen(configfile,"r"))){
286 while (NULL != (linebuffer = agets(file))) {
287 if (linebuffer[0] == '\0') {
291 parse_line(linebuffer,&token,&value);
294 if (token != NUMDRIVE){
295 init_changer_struct(chg, numconf);
297 numconf = atoi(value);
298 init_changer_struct(chg, numconf);
303 case NUMDRIVE: if (atoi(value) != numconf)
304 fprintf(stderr,"Error: number_drives at wrong place, should be "\
308 chg->eject = atoi(value);
311 chg->sleep = atoi(value);
314 chg->device = stralloc(value);
317 chg->conf[drivenum].scsitapedev = stralloc(value);
320 chg->conf[drivenum].tapestatfile = stralloc(value);
323 chg->cleanmax = atoi(value);
326 drivenum = atoi(value);
327 if(drivenum >= numconf){
328 fprintf(stderr,"Error: drive must be less than number_drives\n");
332 if (drivenum < numconf){
333 chg->conf[drivenum].drivenum = atoi(value);
335 fprintf(stderr,"Error: drive is not less than number_drives"\
336 " drivenum ignored\n");
340 if (drivenum < numconf){
341 chg->conf[drivenum].start = atoi(value);
343 fprintf(stderr,"Error: drive is not less than number_drives"\
344 " startuse ignored\n");
348 if (drivenum < numconf){
349 chg->conf[drivenum].end = atoi(value);
351 fprintf(stderr,"Error: drive is not less than number_drives"\
352 " enduse ignored\n");
356 if (drivenum < numconf){
357 chg->conf[drivenum].cleanslot = atoi(value);
359 fprintf(stderr,"Error: drive is not less than number_drives"\
360 " cleanslot ignored\n");
364 if (drivenum < numconf){
365 chg->conf[drivenum].device = stralloc(value);
367 fprintf(stderr,"Error: drive is not less than number_drives"\
368 " device ignored\n");
372 if (drivenum < numconf){
373 chg->conf[drivenum].slotfile = stralloc(value);
375 fprintf(stderr,"Error: drive is not less than number_drives"\
376 " slotfile ignored\n");
380 if (drivenum < numconf){
381 chg->conf[drivenum].cleanfile = stralloc(value);
383 fprintf(stderr,"Error: drive is not less than number_drives"\
384 " cleanfile ignored\n");
388 if (drivenum < numconf){
389 chg->conf[drivenum].timefile = stralloc(value);
391 fprintf(stderr,"Error: drive is not less than number_drives"\
392 " usagecount ignored\n");
396 fprintf(stderr,"Error: Unknown token\n");
408 /*----------------------------------------------------------------------------*/
410 /* The tape drive does not have an idea of current slot so
411 * we use a file to store the current slot. It is not ideal
412 * but it gets the job done
414 int get_current_slot(char *count_file)
418 if ((inf=fopen(count_file,"r")) == NULL) {
419 fprintf(stderr, "%s: unable to open current slot file (%s)\n",
420 get_pname(), count_file);
424 if (fscanf(inf, "%d", &retval) != 1) {
425 fprintf(stderr, "%s: unable to read current slot file (%s)\n",
426 get_pname(), count_file);
434 void put_current_slot(char *count_file,int slot)
438 if ((inf=fopen(count_file,"w")) == NULL) {
439 fprintf(stderr, "%s: unable to open current slot file (%s)\n",
440 get_pname(), count_file);
443 fprintf(inf, "%d\n", slot);
447 /* ----------------------------------------------------------------------
448 This stuff deals with parsing the command line */
450 typedef struct com_arg
458 typedef struct com_stru
465 /* major command line args */
472 argument argdefs[]={{"-slot",COM_SLOT,1},
473 {"-info",COM_INFO,0},
474 {"-reset",COM_RESET,0},
475 {"-eject",COM_EJECT,0},
476 {"-clean",COM_CLEAN,0}};
479 /* minor command line args */
486 argument slotdefs[]={{"current",SLOT_CUR,0},
487 {"next",SLOT_NEXT,0},
488 {"prev",SLOT_PREV,0},
489 {"first",SLOT_FIRST,0},
490 {"last",SLOT_LAST,0}};
492 int is_positive_number(char *tmp) /* is the string a valid positive int? */
495 if ((tmp==NULL)||(tmp[0]==0))
497 while ((tmp[i]>='0')&&(tmp[i]<='9')&&(tmp[i]!=0))
505 void usage(char *argv[])
508 printf("%s: Usage error.\n", argv[0]);
509 for (cnt=0; cnt < COMCOUNT; cnt++){
510 printf(" %s %s",argv[0],argdefs[cnt].str);
511 if (argdefs[cnt].takesparam)
512 printf(" <param>\n");
520 void parse_args(int argc, char *argv[],command *rval)
523 if ((argc < 2) || (argc > 3)) {
528 while ((i<COMCOUNT)&&(strcmp(argdefs[i].str,argv[1])))
534 rval->command_code = argdefs[i].command_code;
535 if (argdefs[i].takesparam) {
540 rval->parameter=argv[2];
551 /* used to find actual slot number from keywords next, prev, first, etc */
564 (void)loaded; /* Quiet unused warning */
565 if (changer_file != NULL)
567 current_slot=get_current_slot(changer_file);
569 current_slot = GetCurrentSlot(fd, 0);
571 if (current_slot > maxslot){
572 current_slot = slot_offset;
574 if (current_slot < slot_offset){
575 current_slot = slot_offset;
579 while((i < SLOTCOUNT) && (strcmp(slotdefs[i].str,parameter)))
587 if (++current_slot==nslots+slot_offset)
592 if (--current_slot<slot_offset)
603 printf("<none> no slot `%s'\n",parameter);
612 * This function should ask the drive if it wants to be cleaned
618 return get_clean_state(tapedev);
622 * This function should move the cleaning cartridge into the drive
636 if (cleancart == -1 ){
640 /* Now we should increment the counter */
641 if (cnt_file != NULL){
642 counter = get_current_slot(cnt_file);
644 if (counter>=maxclean){
645 /* Now we should inform the administrator */
648 int mail_pipe_opened = 1;
649 if(getconf_seen(CNF_MAILTO) && strlen(getconf_str(CNF_MAILTO)) > 0 &&
650 validate_mailto(getconf_str(CNF_MAILTO))) {
651 mail_cmd = vstralloc(MAILER,
652 " -s", " \"", "AMANDA PROBLEM: PLEASE FIX", "\"",
653 " ", getconf_str(CNF_MAILTO),
655 if((mailf = popen(mail_cmd, "w")) == NULL){
656 printf("Mail failed\n");
657 error("could not open pipe to \"%s\": %s",
658 mail_cmd, strerror(errno));
663 mail_pipe_opened = 0;
665 fprintf(mailf, "\nNo mail recipient specified, output redirected to stderr");
668 fprintf(mailf,"\nThe usage count of your cleaning tape in slot %d",
670 fprintf(mailf,"\nis more than %d. (cleanmax)",maxclean);
671 fprintf(mailf,"\nTapedrive %s needs to be cleaned",tapedev);
672 fprintf(mailf,"\nPlease insert a new cleaning tape and reset");
673 fprintf(mailf,"\nthe countingfile %s",cnt_file);
675 if(mail_pipe_opened == 1 && pclose(mailf) != 0) {
676 error("mail command failed: %s", mail_cmd);
683 put_current_slot(cnt_file,counter);
685 load(fd,drivenum,cleancart);
687 if (drive_loaded(fd, drivenum))
688 unload(fd,drivenum,cleancart);
691 /* ----------------------------------------------------------------------*/
701 command com; /* a little DOS joke */
704 * drive_num really should be something from the config file, but..
705 * for now, it is set to zero, since most of the common changers
706 * used by amanda only have one drive ( until someone wants to
707 * use an EXB60/120, or a Breece Hill Q45.. )
710 int need_eject = 0; /* Does the drive need an eject command ? */
711 unsigned need_sleep = 0; /* How many seconds to wait for the drive to get ready */
714 char *clean_file=NULL;
715 char *time_file=NULL;
721 int fd, slotcnt, drivecnt;
723 char *changer_dev = NULL;
724 char *tape_device = NULL;
725 char *changer_file = NULL;
726 char *scsitapedevice = NULL;
728 set_pname("chg-scsi");
730 /* Don't die when child closes pipe */
731 signal(SIGPIPE, SIG_IGN);
733 dbopen(DBG_SUBDIR_SERVER);
734 parse_args(argc,argv,&com);
736 changer = alloc(SIZEOF(changer_t));
737 if(read_conffile(CONFFILE_NAME)) {
738 fprintf(stderr, "%s: could not find config file \"%s\"",
739 changer_dev, conffile);
743 changer_dev = getconf_str(CNF_CHNGRDEV);
744 changer_file = getconf_str(CNF_CHNGRFILE);
745 tape_device = getconf_str(CNF_TAPEDEV);
747 /* Get the configuration parameters */
749 if (strlen(tape_device)==1){
750 read_config(changer_file, changer);
751 confnum=atoi(tape_device);
752 use_slots = changer->conf[confnum].end-changer->conf[confnum].start+1;
753 slot_offset = changer->conf[confnum].start;
754 drive_num = changer->conf[confnum].drivenum;
755 need_eject = changer->eject;
756 need_sleep = changer->sleep;
757 clean_file = stralloc(changer->conf[confnum].cleanfile);
758 clean_slot = changer->conf[confnum].cleanslot;
759 maxclean = changer->cleanmax;
760 if (NULL != changer->conf[confnum].timefile)
761 time_file = stralloc(changer->conf[confnum].timefile);
762 if (NULL != changer->conf[confnum].slotfile)
763 changer_file = stralloc(changer->conf[confnum].slotfile);
766 if (NULL != changer->conf[confnum].device)
767 tape_device = stralloc(changer->conf[confnum].device);
768 if (NULL != changer->device)
769 changer_dev = stralloc(changer->device);
770 if (NULL != changer->conf[confnum].scsitapedev)
771 scsitapedevice = stralloc(changer->conf[confnum].scsitapedev);
772 if (NULL != changer->conf[confnum].tapestatfile)
773 tapestatfile = stralloc(changer->conf[confnum].tapestatfile);
774 dump_changer_struct(changer);
775 /* get info about the changer */
776 fd = OpenDevice(INDEX_CHANGER , changer_dev,
777 "changer_dev", changer->conf[confnum].changerident);
779 int localerr = errno;
780 fprintf(stderr, "%s: open: %s: %s\n", get_pname(),
781 changer_dev, strerror(localerr));
782 printf("%s open: %s: %s\n", "<none>", changer_dev, strerror(localerr));
783 dbprintf(("%s: open: %s: %s\n", get_pname(),
784 changer_dev, strerror(localerr)));
788 if (tape_device == NULL)
790 tape_device = stralloc(changer_dev);
793 if (scsitapedevice == NULL)
795 scsitapedevice = stralloc(tape_device);
798 if ((changer->conf[confnum].end == -1) || (changer->conf[confnum].start == -1)){
799 slotcnt = get_slot_count(fd);
803 free_changer_struct(&changer);
805 /* get info about the changer */
807 fd = OpenDevice(INDEX_CHANGER , changer_dev,
808 "changer_dev", changer->conf[confnum].changerident);
810 int localerr = errno;
811 fprintf(stderr, "%s: open: %s: %s\n", get_pname(),
812 changer_dev, strerror(localerr));
813 printf("%s open: %s: %s\n", "<none>", changer_dev, strerror(localerr));
814 dbprintf(("%s: open: %s: %s\n", get_pname(),
815 changer_dev, strerror(localerr)));
818 slotcnt = get_slot_count(fd);
826 drivecnt = get_drive_count(fd);
828 if (drive_num > drivecnt) {
829 printf("%s drive number error (%d > %d)\n", "<none>",
830 drive_num, drivecnt);
831 fprintf(stderr, "%s: requested drive number (%d) greater than "
832 "number of supported drives (%d)\n", get_pname(),
833 drive_num, drivecnt);
834 dbprintf(("%s: requested drive number (%d) greater than "
835 "number of supported drives (%d)\n", get_pname(),
836 drive_num, drivecnt));
841 loaded = drive_loaded(fd, drive_num);
843 switch(com.command_code) {
844 case COM_SLOT: /* slot changing command */
845 if (is_positive_number(com.parameter)) {
846 if ((target = atoi(com.parameter))>=use_slots) {
847 printf("<none> no slot `%d'\n",target);
852 target = target+slot_offset;
855 target=get_relative_target(fd, use_slots,
858 changer_file,slot_offset,slot_offset+use_slots);
860 if (changer_file != NULL)
862 oldtarget=get_current_slot(changer_file);
864 oldtarget = GetCurrentSlot(fd, drive_num);
866 if ((oldtarget)!=target) {
868 eject_tape(scsitapedevice, need_eject);
869 (void)unload(fd, drive_num, oldtarget);
870 if (ask_clean(scsitapedevice))
871 clean_tape(fd,tape_device,clean_file,drive_num,
872 clean_slot,maxclean,time_file);
876 if (changer_file != NULL)
878 put_current_slot(changer_file, target);
880 if (!loaded && isempty(fd, target)) {
881 printf("%d slot %d is empty\n",target-slot_offset,
888 if (load(fd, drive_num, target) != 0) {
889 printf("%d slot %d move failed\n",target-slot_offset,
896 Tape_Ready(scsitapedevice, need_sleep);
897 printf("%d %s\n", target-slot_offset, tape_device);
901 if (changer_file != NULL)
903 printf("%d ", get_current_slot(changer_file)-slot_offset);
905 printf("%d ", GetCurrentSlot(fd, drive_num)-slot_offset);
907 printf("%d 1\n", use_slots);
911 if (changer_file != NULL)
913 target=get_current_slot(changer_file);
915 target = GetCurrentSlot(fd, drive_num);
918 if (!isempty(fd, target))
919 target=find_empty(fd,0 ,0);
921 eject_tape(scsitapedevice, need_eject);
922 (void)unload(fd, drive_num, target);
923 if (ask_clean(scsitapedevice))
924 clean_tape(fd,tape_device,clean_file,drive_num,clean_slot,
928 if (isempty(fd, slot_offset)) {
929 printf("0 slot 0 is empty\n");
935 if (load(fd, drive_num, slot_offset) != 0) {
936 printf("%d slot %d move failed\n",slot_offset,
942 if (changer_file != NULL)
944 put_current_slot(changer_file, slot_offset);
947 Tape_Ready(scsitapedevice, need_sleep);
948 if (changer_file != NULL)
950 printf("%d %s\n", get_current_slot(changer_file), tape_device);
952 printf("%d %s\n", GetCurrentSlot(fd, drive_num), tape_device);
958 if (changer_file != NULL)
960 target=get_current_slot(changer_file);
962 target = GetCurrentSlot(fd, drive_num);
965 eject_tape(scsitapedevice, need_eject);
966 (void)unload(fd, drive_num, target);
967 if (ask_clean(scsitapedevice))
968 clean_tape(fd,tape_device,clean_file,drive_num,clean_slot,
970 printf("%d %s\n", target, tape_device);
972 printf("%d %s\n", target, "drive was not loaded");
978 if (changer_file != NULL)
980 target=get_current_slot(changer_file);
982 target = GetCurrentSlot(fd, drive_num);
985 eject_tape(scsitapedevice, need_eject);
986 (void)unload(fd, drive_num, target);
988 clean_tape(fd,tape_device,clean_file,drive_num,clean_slot,
990 printf("%s cleaned\n", tape_device);
1000 * indent-tabs-mode: nil