root/fs/proc/scsi.c

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

DEFINITIONS

This source file includes following definitions.
  1. count_dir_entries
  2. proc_lookupscsi
  3. proc_readscsidir
  4. get_not_present_info
  5. proc_readscsi
  6. proc_writescsi
  7. proc_scsilseek

   1 /*
   2  *  linux/fs/proc/scsi.c  
   3  *  (c) 1995 Michael Neuffer neuffer@goofy.zdv.uni-mainz.de
   4  *
   5  *  The original version was derived from linux/fs/proc/net.c,
   6  *  which is Copyright (C) 1991, 1992 Linus Torvalds. 
   7  *  Much has been rewritten, but some of the code still remains.
   8  *
   9  *  /proc/scsi directory handling functions
  10  *
  11  *  last change: 95/07/04    
  12  *
  13  *  Initial version: March '95
  14  *  95/05/15 Added subdirectories for each driver and show every
  15  *           registered HBA as a single file. 
  16  *  95/05/30 Added rudimentary write support for parameter passing
  17  *  95/07/04 Fixed bugs in directory handling
  18  *
  19  *  TODO: Improve support to write to the driver files
  20  *        Optimize directory handling 
  21  *        Add some more comments
  22  */
  23 #include <linux/autoconf.h>
  24 #include <asm/segment.h>
  25 #include <linux/errno.h>
  26 #include <linux/sched.h>
  27 #include <linux/proc_fs.h>
  28 #include <linux/stat.h>
  29 #include <linux/config.h>
  30 #include <linux/mm.h>
  31 
  32 /* forward references */
  33 static int proc_readscsi(struct inode * inode, struct file * file,
  34                          char * buf, int count);
  35 static int proc_writescsi(struct inode * inode, struct file * file,
  36                          char * buf, int count);
  37 static int proc_readscsidir(struct inode *, struct file *, 
  38                             void *, filldir_t filldir);
  39 static int proc_lookupscsi(struct inode *,const char *,int,struct inode **);
  40 static int proc_scsilseek(struct inode *, struct file *, off_t, int);
  41 
  42 extern uint count_templates(void);
  43 extern void build_proc_dir_hba_entries(uint);
  44 
  45 /* the *_get_info() functions are in the respective scsi driver code */
  46 extern int (* dispatch_scsi_info_ptr)(int, char *, char **, off_t, int, int);
  47     
  48     
  49 static struct file_operations proc_scsi_operations = {
  50     proc_scsilseek,     /* lseek   */
  51     proc_readscsi,      /* read    */
  52     proc_writescsi,     /* write   */
  53     proc_readscsidir,   /* readdir */
  54     NULL,               /* select  */
  55     NULL,               /* ioctl   */
  56     NULL,               /* mmap    */
  57     NULL,               /* no special open code    */
  58     NULL,               /* no special release code */
  59     NULL                /* can't fsync */
  60 };
  61 
  62 /*
  63  * proc directories can do almost nothing..
  64  */
  65 struct inode_operations proc_scsi_inode_operations = {
  66     &proc_scsi_operations,  /* default scsi directory file-ops */
  67     NULL,           /* create      */
  68     proc_lookupscsi,/* lookup      */
  69     NULL,           /* link        */
  70     NULL,           /* unlink      */
  71     NULL,           /* symlink     */
  72     NULL,           /* mkdir       */
  73     NULL,           /* rmdir       */
  74     NULL,           /* mknod       */
  75     NULL,           /* rename      */
  76     NULL,           /* readlink    */
  77     NULL,           /* follow_link */
  78     NULL,           /* bmap        */
  79     NULL,           /* truncate    */
  80     NULL            /* permission  */
  81 };
  82 
  83 struct proc_dir_entry scsi_dir[PROC_SCSI_FILE - PROC_SCSI_SCSI + 3]; 
  84 struct proc_dir_entry scsi_hba_dir[(PROC_SCSI_LAST - PROC_SCSI_FILE) * 4]; 
  85 
  86 static struct proc_dir_entry scsi_dir2[] = {
  87     { PROC_SCSI,                 1, "." },
  88     { PROC_ROOT_INO,             2, ".." },
  89     { PROC_SCSI_NOT_PRESENT,    11, "not.present" },
  90     { 0, 0, NULL }
  91 };
  92 
  93 inline static uint count_dir_entries(uint inode, uint *num)
     /* [previous][next][first][last][top][bottom][index][help] */
  94 {
  95     struct proc_dir_entry *dir;
  96     uint index, flag;
  97 
  98     (uint) *num = flag = index = 0;    
  99     
 100     if(dispatch_scsi_info_ptr) {
 101         if (inode == PROC_SCSI) { 
 102             dir = scsi_dir;
 103             while(dir[(uint)*num].low_ino)
 104                 (*num)++;
 105         } else {
 106             /* Here we do not simply count the entries. Since the array
 107              * contains the directories of all drivers, we need to return
 108              * a pointer to the beginning of the directory information
 109              * and its length.
 110              */
 111             dir = scsi_hba_dir;
 112             while(dir[index].low_ino || dir[index].low_ino <= PROC_SCSI_LAST) {
 113                 if(dir[index].low_ino == inode)
 114                     flag = 1;
 115                 if(dir[index].low_ino == 0) {
 116                     if(flag == 1)
 117                         break;
 118                     else
 119                         *num = 0;
 120                 } else {
 121                     (*num)++;
 122                 }
 123                 index++;
 124             }
 125             return(index - (*num));
 126         }
 127     }
 128     else {
 129         dir = scsi_dir2;
 130         while(dir[(uint)*num].low_ino)
 131             (*num)++;
 132     }   
 133     return(0);
 134 }
 135 
 136 static int proc_lookupscsi(struct inode * dir, const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 137                            struct inode ** result)
 138 {
 139     struct proc_dir_entry *de = NULL;
 140 
 141     *result = NULL;
 142     if (!dir)
 143         return(-ENOENT);
 144     if (!S_ISDIR(dir->i_mode)) {
 145         iput(dir);
 146         return(-ENOENT);
 147     }
 148     if (dispatch_scsi_info_ptr != NULL) {
 149         if (dir->i_ino <= PROC_SCSI_SCSI)
 150             de = scsi_dir;
 151         else {
 152             de = &scsi_hba_dir[dispatch_scsi_info_ptr(dir->i_ino, 0, 0, 0, 0, 2)];
 153         }
 154     }
 155     else
 156         de = scsi_dir2;
 157     
 158     for (; de->name ; de++) {
 159         if (!proc_match(len, name, de))
 160             continue;
 161         *result = iget(dir->i_sb, de->low_ino);
 162         iput(dir);
 163         if (!*result)
 164             return(-ENOENT);
 165         return(0);
 166     }
 167     iput(dir);
 168     return(-ENOENT);
 169 }
 170 
 171 static int proc_readscsidir(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 172                             void * dirent, filldir_t filldir)
 173 {
 174     struct proc_dir_entry * de;
 175     uint index, num;
 176  
 177     num = 0;
 178 
 179     if (!inode || !S_ISDIR(inode->i_mode))
 180         return(-EBADF);
 181 
 182     index = count_dir_entries(inode->i_ino, &num);
 183 
 184     while (((unsigned) filp->f_pos + index) < index + num) {
 185         if (dispatch_scsi_info_ptr) {
 186             if (inode->i_ino <= PROC_SCSI_SCSI)
 187                 de = scsi_dir + filp->f_pos;
 188             else
 189                 de = scsi_hba_dir + filp->f_pos + index;
 190         }
 191         else {
 192             de = scsi_dir2 + filp->f_pos;
 193         }
 194         if (filldir(dirent, de->name, de->namelen, filp->f_pos, de->low_ino)<0)
 195             break;
 196         filp->f_pos++;
 197     }
 198     return(0);
 199 }
 200 
 201 int get_not_present_info(char *buffer, char **start, off_t offset, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
 202 {
 203     int len, pos, begin;
 204     
 205     begin = 0;
 206     pos = len = sprintf(buffer, 
 207                         "No low-level scsi modules are currently present\n");
 208     if(pos < offset) {
 209         len = 0;
 210         begin = pos;
 211     }
 212     
 213     *start = buffer + (offset - begin);   /* Start of wanted data */
 214     len -= (offset - begin);
 215     if(len > length)
 216         len = length;
 217     
 218     return(len);
 219 }
 220 
 221 #define PROC_BLOCK_SIZE (3*1024)     /* 4K page size, but our output routines 
 222                                       * use some slack for overruns 
 223                                       */
 224 
 225 static int proc_readscsi(struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 226                          char * buf, int count)
 227 {
 228     int length;
 229     int bytes = count;
 230     int copied = 0;
 231     int thistime;
 232     char * page;
 233     char * start;
 234     
 235     if (count < -1)               /* Normally I wouldn't do this, */ 
 236         return(-EINVAL);          /* but it saves some redundant code.
 237                                    * Now it is possible to seek to the 
 238                                    * end of the file */
 239     if (!(page = (char *) __get_free_page(GFP_KERNEL)))
 240         return(-ENOMEM);
 241     
 242     while(bytes > 0 || count == -1) {   
 243         thistime = bytes;
 244         if(bytes > PROC_BLOCK_SIZE || count == -1)
 245             thistime = PROC_BLOCK_SIZE;
 246         
 247         if(dispatch_scsi_info_ptr)
 248             length = dispatch_scsi_info_ptr(inode->i_ino, page, &start, 
 249                                             file->f_pos, thistime, 0);
 250         else
 251             length = get_not_present_info(page, &start, file->f_pos, thistime);
 252         if(length < 0) {
 253             free_page((ulong) page);
 254             return(length);
 255         }
 256         
 257         /*
 258          *  We have been given a non page aligned block of
 259          *  the data we asked for + a bit. We have been given
 260          *  the start pointer and we know the length.. 
 261          */
 262         if (length <= 0)
 263             break;
 264         /*
 265          *  Copy the bytes, if we're not doing a seek to 
 266          *      the end of the file 
 267          */
 268         if (count != -1)
 269             memcpy_tofs(buf + copied, start, length);
 270         file->f_pos += length;  /* Move down the file */
 271         bytes -= length;
 272         copied += length;
 273         
 274         if(length < thistime)
 275             break;  /* End of file */
 276         
 277     }
 278     
 279     free_page((ulong) page);
 280     return(copied);
 281 }
 282 
 283 
 284 static int proc_writescsi(struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 285                          char * buf, int count)
 286 {
 287     int ret = 0;
 288     char * page;
 289     
 290     if (!(page = (char *) __get_free_page(GFP_KERNEL)))
 291         return(-ENOMEM);
 292 
 293     if(count > PROC_BLOCK_SIZE) {
 294         return(-EOVERFLOW);
 295     }
 296 
 297     if(dispatch_scsi_info_ptr != NULL) {
 298         memcpy_fromfs(page, buf, count);
 299         ret = dispatch_scsi_info_ptr(inode->i_ino, page, 0, 0, count, 1);
 300     } else {
 301         free_page((ulong) page);   
 302         return(-ENOPKG);          /* Nothing here */
 303     }
 304     
 305     free_page((ulong) page);
 306     return(ret);
 307 }
 308 
 309 
 310 static int proc_scsilseek(struct inode * inode, struct file * file, 
     /* [previous][next][first][last][top][bottom][index][help] */
 311                           off_t offset, int orig)
 312 {
 313     switch (orig) {
 314     case 0:
 315         file->f_pos = offset;
 316         return(file->f_pos);
 317     case 1:
 318         file->f_pos += offset;
 319         return(file->f_pos);
 320     case 2:                  /* This ugly hack allows us to    */
 321         if (offset)          /* to determine the length of the */
 322             return(-EINVAL); /* file and then later savely to  */ 
 323         proc_readscsi(inode, file, 0, -1); /* seek in it       */ 
 324         return(file->f_pos);
 325     default:
 326         return(-EINVAL);
 327     }
 328 }
 329 
 330 /*
 331  * Overrides for Emacs so that we almost follow Linus's tabbing style.
 332  * Emacs will notice this stuff at the end of the file and automatically
 333  * adjust the settings for this buffer only.  This must remain at the end
 334  * of the file.
 335  * ---------------------------------------------------------------------------
 336  * Local variables:
 337  * c-indent-level: 4
 338  * c-brace-imaginary-offset: 0
 339  * c-brace-offset: -4
 340  * c-argdecl-indent: 4
 341  * c-label-offset: -4
 342  * c-continued-statement-offset: 4
 343  * c-continued-brace-offset: 0
 344  * indent-tabs-mode: nil
 345  * tab-width: 8
 346  * End:
 347  */

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