root/drivers/scsi/scsi_proc.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get_hba_index
  2. generic_proc_info
  3. dispatch_scsi_info
  4. count_templates
  5. build_proc_dir_hba_entries
  6. build_proc_dir_entries
  7. parseFree
  8. parseInit
  9. parseOpt

   1 /*
   2  * linux/drivers/scsi/scsi_proc.c
   3  *
   4  * The functions in this file provide an interface between
   5  * the PROC file system and the SCSI device drivers
   6  * It is mainly used for debugging, statistics and to pass 
   7  * information directly to the lowlevel driver.
   8  *
   9  * (c) 1995 Michael Neuffer neuffer@goofy.zdv.uni-mainz.de 
  10  * Version: 0.99.6   last change: 95/07/04
  11  * 
  12  * generic command parser provided by: 
  13  * Andreas Heilwagen <crashcar@informatik.uni-koblenz.de>
  14  */
  15 
  16 #ifdef MODULE
  17 /*
  18  * Don't import our own symbols, as this would severely mess up our
  19  * symbol tables.
  20  */
  21 #define _SCSI_SYMS_VER_
  22 #include <linux/autoconf.h>
  23 #include <linux/module.h>
  24 #include <linux/version.h>
  25 #else
  26 #define MOD_INC_USE_COUNT
  27 #define MOD_DEC_USE_COUNT
  28 #endif
  29 
  30 #include <linux/string.h>
  31 #include <linux/mm.h>
  32 #include <linux/malloc.h>
  33 #include <linux/proc_fs.h>
  34 #include <linux/errno.h>
  35 #include "../block/blk.h"
  36 #include "scsi.h"
  37 #include "hosts.h"
  38 
  39 #ifndef TRUE
  40 #define TRUE  1
  41 #define FALSE 0
  42 #endif
  43 
  44 extern struct proc_dir_entry scsi_dir[];
  45 extern struct proc_dir_entry scsi_hba_dir[];
  46 extern int scsi_proc_info(char *, char **, off_t, int, int, int);
  47  
  48 
  49 int get_hba_index(int ino)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 {
  51     Scsi_Host_Template *tpnt = scsi_hosts;
  52     struct Scsi_Host *hpnt = scsi_hostlist;
  53     uint x = 0;
  54 
  55     /*
  56      * Danger - this has massive race conditions in it.
  57      * If the someone adds/removes entries from the scsi chain
  58      * while someone else is looking at /proc/scsi, unpredictable
  59      * results will be obtained.
  60      */
  61     while (tpnt) {
  62         if (ino == tpnt->low_ino) 
  63                 return(x);
  64         x += 3;
  65         while (hpnt) {
  66             if(hpnt->hostt == tpnt) /* This gives us the correct index */
  67                 x++;
  68             hpnt = hpnt->next;
  69         }
  70         tpnt = tpnt->next;
  71     }
  72     return(0);
  73 }
  74 
  75 /* generic_proc_info
  76  * Used if the driver currently has no own support for /proc/scsi
  77  */
  78 int generic_proc_info(char *buffer, char **start, off_t offset, 
     /* [previous][next][first][last][top][bottom][index][help] */
  79                      int length, int inode, int inout)
  80 {
  81     int len, pos, begin;
  82 
  83     if(inout == TRUE)
  84         return(-ENOSYS);  /* This is a no-op */
  85     
  86     begin = 0;
  87     pos = len = sprintf(buffer, 
  88                         "The driver does not yet support the proc-fs\n");
  89     if(pos < offset) {
  90         len = 0;
  91         begin = pos;
  92     }
  93     
  94     *start = buffer + (offset - begin);   /* Start of wanted data */
  95     len -= (offset - begin);
  96     if(len > length)
  97         len = length;
  98     
  99     return(len);
 100 }
 101 
 102 /* dispatch_scsi_info is the central dispatcher 
 103  * It is the interface between the proc-fs and the SCSI subsystem code
 104  */
 105 extern int dispatch_scsi_info(int ino, char *buffer, char **start, 
     /* [previous][next][first][last][top][bottom][index][help] */
 106                               off_t offset, int length, int func)
 107 {
 108     int retval;
 109     struct Scsi_Host *hpnt = scsi_hostlist;
 110 
 111     if(func != 2) {    
 112         if(ino == PROC_SCSI_SCSI) {            
 113             /*
 114              * This is for the scsi core, rather than any specific
 115              * lowlevel driver.
 116              */
 117             return(scsi_proc_info(buffer, start, offset, length, 0, func));
 118         }
 119 
 120         while(hpnt) {
 121             if (ino == (hpnt->host_no + PROC_SCSI_FILE)) {
 122                 if(hpnt->hostt->proc_info == NULL)
 123                     return generic_proc_info(buffer, start, offset, length, 
 124                                              hpnt->host_no, func);
 125                 else
 126                     return(hpnt->hostt->proc_info(buffer, start, offset, 
 127                                                   length, hpnt->host_no, func));
 128             }
 129             hpnt = hpnt->next;
 130         }
 131         return(-EBADF);
 132     } else
 133         return(get_hba_index(ino));
 134 }
 135 
 136 inline uint count_templates(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 137 {
 138     Scsi_Host_Template *tpnt = scsi_hosts;
 139     uint x = 0;
 140     
 141     while (tpnt) {
 142         tpnt = tpnt->next;
 143         x++;
 144     }
 145     return (x);
 146 }
 147 
 148 void build_proc_dir_hba_entries(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 149 {
 150     Scsi_Host_Template *tpnt = scsi_hosts;
 151     struct Scsi_Host *hpnt;
 152     uint x, y;
 153 
 154     /* namespace for 16 HBAs with host_no 0-999 
 155      * I don't think we'll need more. Having more than 2 
 156      * HBAs in one system is already highly unusual 
 157      */
 158     static char names[PROC_SCSI_LAST - PROC_SCSI_FILE][4];
 159     
 160     x = y = 0;
 161 
 162     while (tpnt) {
 163         scsi_hba_dir[x].low_ino = tpnt->low_ino;
 164         scsi_hba_dir[x].namelen = 1;
 165         scsi_hba_dir[x++].name = ".";
 166         scsi_hba_dir[x].low_ino = PROC_SCSI;
 167         scsi_hba_dir[x].namelen = 2;
 168         scsi_hba_dir[x++].name = "..";
 169 
 170         hpnt = scsi_hostlist;
 171         while (hpnt) {
 172             if (tpnt == hpnt->hostt) {
 173                 scsi_hba_dir[x].low_ino = PROC_SCSI_FILE + hpnt->host_no;
 174                 scsi_hba_dir[x].namelen = sprintf(names[y],"%d",hpnt->host_no);
 175                 scsi_hba_dir[x].name = names[y];
 176                 y++;
 177                 x++;
 178             }
 179             hpnt = hpnt->next;
 180         }
 181 
 182         scsi_hba_dir[x].low_ino = 0;
 183         scsi_hba_dir[x].namelen = 0;
 184         scsi_hba_dir[x++].name = NULL;
 185         tpnt = tpnt->next;
 186     }    
 187 }
 188 
 189 void build_proc_dir_entries(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 190 {
 191     Scsi_Host_Template *tpnt = scsi_hosts;
 192     
 193     uint newnum; 
 194     uint x;
 195     
 196     newnum = count_templates();
 197     
 198     scsi_dir[0].low_ino = PROC_SCSI;
 199     scsi_dir[0].namelen = 1;
 200     scsi_dir[0].name = ".";
 201     scsi_dir[1].low_ino = PROC_ROOT_INO;
 202     scsi_dir[1].namelen = 2;
 203     scsi_dir[1].name = "..";
 204     scsi_dir[2].low_ino = PROC_SCSI_SCSI;
 205     scsi_dir[2].namelen = 4;
 206     scsi_dir[2].name = "scsi";
 207 
 208     for(x = 3; x < newnum + 3; x++, tpnt = tpnt->next) { 
 209         scsi_dir[x].low_ino = tpnt->low_ino;
 210         scsi_dir[x].namelen = strlen(tpnt->procname);
 211         scsi_dir[x].name = tpnt->procname;
 212     }
 213 
 214     scsi_dir[x].low_ino = 0;
 215     scsi_dir[x].namelen = 0;
 216     scsi_dir[x].name = NULL;
 217     
 218     build_proc_dir_hba_entries();
 219 }
 220 
 221 
 222 /*
 223  *  parseHandle *parseInit(char *buf, char *cmdList, int cmdNum); 
 224  *              gets a pointer to a null terminated data buffer
 225  *              and a list of commands with blanks as delimiter 
 226  *      in between. 
 227  *      The commands have to be alphanumerically sorted. 
 228  *      cmdNum has to contain the number of commands.
 229  *              On success, a pointer to a handle structure
 230  *              is returned, NULL on failure
 231  *
 232  *      int parseOpt(parseHandle *handle, char **param);
 233  *              processes the next parameter. On success, the
 234  *              index of the appropriate command in the cmdList
 235  *              is returned, starting with zero.
 236  *              param points to the null terminated parameter string.
 237  *              On failure, -1 is returned.
 238  *
 239  *      The databuffer buf may only contain pairs of commands
 240  *          options, separated by blanks:
 241  *              <Command> <Parameter> [<Command> <Parameter>]*
 242  */
 243 
 244 typedef struct
 245 {
 246     char *buf,                             /* command buffer  */
 247          *cmdList,                         /* command list    */
 248          *bufPos,                          /* actual position */
 249          **cmdPos,                         /* cmdList index   */
 250          cmdNum;                           /* cmd number      */
 251 } parseHandle;
 252         
 253 
 254 inline int parseFree (parseHandle *handle)               /* free memory     */
     /* [previous][next][first][last][top][bottom][index][help] */
 255 {
 256     kfree (handle->cmdPos);
 257     kfree (handle);
 258     
 259     return(-1);
 260 }
 261 
 262         
 263 parseHandle *parseInit(char *buf, char *cmdList, int cmdNum)
     /* [previous][next][first][last][top][bottom][index][help] */
 264 {
 265     char        *ptr;                               /* temp pointer    */
 266     parseHandle *handle;                            /* new handle      */
 267     
 268     if (!buf || !cmdList)                           /* bad input ?     */
 269         return(NULL);
 270     if ((handle = (parseHandle*) kmalloc(sizeof(parseHandle), 1)) == 0)
 271         return(NULL);                               /* out of memory   */
 272     if ((handle->cmdPos = (char**) kmalloc(sizeof(int), cmdNum)) == 0) {
 273         kfree(handle);
 274         return(NULL);                               /* out of memory   */
 275     }
 276     
 277     handle->buf     = handle->bufPos = buf;         /* init handle     */
 278     handle->cmdList = cmdList;
 279     handle->cmdNum  = cmdNum;
 280     
 281     handle->cmdPos[cmdNum = 0] = cmdList;
 282     for (ptr = cmdList; *ptr; ptr++) {          /* scan command string */
 283         if(*ptr == ' ') {                       /* and insert zeroes   */
 284             *ptr++ = 0;
 285             handle->cmdPos[++cmdNum] = ptr++;
 286         } 
 287     }
 288     return(handle);
 289 }
 290 
 291 
 292 int parseOpt(parseHandle *handle, char **param)
     /* [previous][next][first][last][top][bottom][index][help] */
 293 {
 294     int  cmdIndex = 0, 
 295          cmdLen = 0;
 296     char *startPos;
 297     
 298     if (!handle)                                    /* invalid handle  */
 299         return(parseFree(handle));
 300     /* skip spaces     */  
 301     for (; *(handle->bufPos) && *(handle->bufPos) == ' '; handle->bufPos++);
 302     if (!*(handle->bufPos))
 303         return(parseFree(handle));                  /* end of data     */
 304     
 305     startPos = handle->bufPos;                      /* store cmd start */
 306     for (; handle->cmdPos[cmdIndex][cmdLen] && *(handle->bufPos); handle->bufPos++)
 307     {                                               /* no string end?  */
 308         for (;;)
 309         {
 310             if (*(handle->bufPos) == handle->cmdPos[cmdIndex][cmdLen])
 311                 break;                              /* char matches ?  */
 312             else
 313                 if (memcmp(startPos, (char*)(handle->cmdPos[++cmdIndex]), cmdLen))
 314                     return(parseFree(handle));      /* unknown command */
 315             
 316             if (cmdIndex >= handle->cmdNum)
 317                 return(parseFree(handle));          /* unknown command */     
 318         }
 319         
 320         cmdLen++;                                   /* next char       */
 321     }
 322     
 323     /* Get param. First skip all blanks, then insert zero after param  */
 324     
 325     for (; *(handle->bufPos) && *(handle->bufPos) == ' '; handle->bufPos++);
 326     *param = handle->bufPos; 
 327     
 328     for (; *(handle->bufPos) && *(handle->bufPos) != ' '; handle->bufPos++);
 329     *(handle->bufPos++) = 0;
 330     
 331     return(cmdIndex);
 332 }
 333 
 334 
 335 /*
 336  * Overrides for Emacs so that we get a uniform tabbing style.
 337  * Emacs will notice this stuff at the end of the file and automatically
 338  * adjust the settings for this buffer only.  This must remain at the end
 339  * of the file.
 340  * ---------------------------------------------------------------------------
 341  * Local variables:
 342  * c-indent-level: 4
 343  * c-brace-imaginary-offset: 0
 344  * c-brace-offset: -4
 345  * c-argdecl-indent: 4
 346  * c-label-offset: -4
 347  * c-continued-statement-offset: 4
 348  * c-continued-brace-offset: 0
 349  * indent-tabs-mode: nil
 350  * tab-width: 8
 351  * End:
 352  */

/* [previous][next][first][last][top][bottom][index][help] */