Loading...
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 | /* * linux/fs/proc/scsi.c * (c) 1995 Michael Neuffer neuffer@goofy.zdv.uni-mainz.de * * The original version was derived from linux/fs/proc/net.c, * which is Copyright (C) 1991, 1992 Linus Torvalds. * Much has been rewritten, but some of the code still remains. * * /proc/scsi directory handling functions * * last change: 95/07/04 * * Initial version: March '95 * 95/05/15 Added subdirectories for each driver and show every * registered HBA as a single file. * 95/05/30 Added rudimentary write support for parameter passing * 95/07/04 Fixed bugs in directory handling * * TODO: Improve support to write to the driver files * Optimize directory handling * Add some more comments */ #include <linux/autoconf.h> #include <asm/segment.h> #include <linux/errno.h> #include <linux/sched.h> #include <linux/proc_fs.h> #include <linux/stat.h> #include <linux/config.h> #include <linux/mm.h> /* forward references */ static int proc_readscsi(struct inode * inode, struct file * file, char * buf, int count); static int proc_writescsi(struct inode * inode, struct file * file, const char * buf, int count); static int proc_readscsidir(struct inode *, struct file *, void *, filldir_t filldir); static int proc_lookupscsi(struct inode *,const char *,int,struct inode **); static int proc_scsilseek(struct inode *, struct file *, off_t, int); extern uint count_templates(void); extern void build_proc_dir_hba_entries(uint); /* the *_get_info() functions are in the respective scsi driver code */ extern int (* dispatch_scsi_info_ptr)(int, char *, char **, off_t, int, int); static struct file_operations proc_scsi_operations = { proc_scsilseek, /* lseek */ proc_readscsi, /* read */ proc_writescsi, /* write */ proc_readscsidir, /* readdir */ NULL, /* select */ NULL, /* ioctl */ NULL, /* mmap */ NULL, /* no special open code */ NULL, /* no special release code */ NULL /* can't fsync */ }; /* * proc directories can do almost nothing.. */ struct inode_operations proc_scsi_inode_operations = { &proc_scsi_operations, /* default scsi directory file-ops */ NULL, /* create */ proc_lookupscsi,/* lookup */ NULL, /* link */ NULL, /* unlink */ NULL, /* symlink */ NULL, /* mkdir */ NULL, /* rmdir */ NULL, /* mknod */ NULL, /* rename */ NULL, /* readlink */ NULL, /* follow_link */ NULL, /* bmap */ NULL, /* truncate */ NULL /* permission */ }; struct proc_dir_entry proc_scsi = { PROC_SCSI, 4, "scsi", S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0, 0, &proc_scsi_inode_operations, NULL, NULL, NULL, &proc_root, NULL }; struct proc_dir_entry scsi_dir[PROC_SCSI_FILE - PROC_SCSI_SCSI + 3]; struct proc_dir_entry scsi_hba_dir[(PROC_SCSI_LAST - PROC_SCSI_FILE) * 4]; static struct proc_dir_entry scsi_dir2[] = { { PROC_SCSI, 1, "." }, { PROC_ROOT_INO, 2, ".." }, { PROC_SCSI_NOT_PRESENT, 11, "not.present" }, { 0, 0, NULL } }; inline static uint count_dir_entries(uint inode, uint *num) { struct proc_dir_entry *dir; uint index, flag; (uint) *num = flag = index = 0; if(dispatch_scsi_info_ptr) { if (inode == PROC_SCSI) { dir = scsi_dir; while(dir[(uint)*num].low_ino) (*num)++; } else { /* Here we do not simply count the entries. Since the array * contains the directories of all drivers, we need to return * a pointer to the beginning of the directory information * and its length. */ dir = scsi_hba_dir; while(dir[index].low_ino || dir[index].low_ino <= PROC_SCSI_LAST) { if(dir[index].low_ino == inode) flag = 1; if(dir[index].low_ino == 0) { if(flag == 1) break; else *num = 0; } else { (*num)++; } index++; } return(index - (*num)); } } else { dir = scsi_dir2; while(dir[(uint)*num].low_ino) (*num)++; } return(0); } static int proc_lookupscsi(struct inode * dir, const char * name, int len, struct inode ** result) { struct proc_dir_entry *de = NULL; *result = NULL; if (!dir) return(-ENOENT); if (!S_ISDIR(dir->i_mode)) { iput(dir); return(-ENOENT); } if (dispatch_scsi_info_ptr != NULL) { if (dir->i_ino <= PROC_SCSI_SCSI) de = scsi_dir; else { de = &scsi_hba_dir[dispatch_scsi_info_ptr(dir->i_ino, 0, 0, 0, 0, 2)]; } } else de = scsi_dir2; for (; de->name ; de++) { if (!proc_match(len, name, de)) continue; *result = proc_get_inode(dir->i_sb, de->low_ino, de); iput(dir); if (!*result) return(-ENOENT); return(0); } iput(dir); return(-ENOENT); } static int proc_readscsidir(struct inode * inode, struct file * filp, void * dirent, filldir_t filldir) { struct proc_dir_entry * de; uint index, num; num = 0; if (!inode || !S_ISDIR(inode->i_mode)) return(-EBADF); index = count_dir_entries(inode->i_ino, &num); while (((unsigned) filp->f_pos + index) < index + num) { if (dispatch_scsi_info_ptr) { if (inode->i_ino <= PROC_SCSI_SCSI) de = scsi_dir + filp->f_pos; else de = scsi_hba_dir + filp->f_pos + index; } else { de = scsi_dir2 + filp->f_pos; } if (filldir(dirent, de->name, de->namelen, filp->f_pos, de->low_ino)<0) break; filp->f_pos++; } return(0); } int get_not_present_info(char *buffer, char **start, off_t offset, int length) { int len, pos, begin; begin = 0; pos = len = sprintf(buffer, "No low-level scsi modules are currently present\n"); if(pos < offset) { len = 0; begin = pos; } *start = buffer + (offset - begin); /* Start of wanted data */ len -= (offset - begin); if(len > length) len = length; return(len); } #define PROC_BLOCK_SIZE (3*1024) /* 4K page size, but our output routines * use some slack for overruns */ static int proc_readscsi(struct inode * inode, struct file * file, char * buf, int count) { int length; int bytes = count; int copied = 0; int thistime; char * page; char * start; if (count < -1) /* Normally I wouldn't do this, */ return(-EINVAL); /* but it saves some redundant code. * Now it is possible to seek to the * end of the file */ if (!(page = (char *) __get_free_page(GFP_KERNEL))) return(-ENOMEM); while(bytes > 0 || count == -1) { thistime = bytes; if(bytes > PROC_BLOCK_SIZE || count == -1) thistime = PROC_BLOCK_SIZE; if(dispatch_scsi_info_ptr) length = dispatch_scsi_info_ptr(inode->i_ino, page, &start, file->f_pos, thistime, 0); else length = get_not_present_info(page, &start, file->f_pos, thistime); if(length < 0) { free_page((ulong) page); return(length); } /* * We have been given a non page aligned block of * the data we asked for + a bit. We have been given * the start pointer and we know the length.. */ if (length <= 0) break; /* * Copy the bytes, if we're not doing a seek to * the end of the file */ if (count != -1) memcpy_tofs(buf + copied, start, length); file->f_pos += length; /* Move down the file */ bytes -= length; copied += length; if(length < thistime) break; /* End of file */ } free_page((ulong) page); return(copied); } static int proc_writescsi(struct inode * inode, struct file * file, const char * buf, int count) { int ret = 0; char * page; if (!(page = (char *) __get_free_page(GFP_KERNEL))) return(-ENOMEM); if(count > PROC_BLOCK_SIZE) { return(-EOVERFLOW); } if(dispatch_scsi_info_ptr != NULL) { memcpy_fromfs(page, buf, count); ret = dispatch_scsi_info_ptr(inode->i_ino, page, 0, 0, count, 1); } else { free_page((ulong) page); return(-ENOPKG); /* Nothing here */ } free_page((ulong) page); return(ret); } static int proc_scsilseek(struct inode * inode, struct file * file, off_t offset, int orig) { switch (orig) { case 0: file->f_pos = offset; return(file->f_pos); case 1: file->f_pos += offset; return(file->f_pos); case 2: /* This ugly hack allows us to */ if (offset) /* to determine the length of the */ return(-EINVAL); /* file and then later savely to */ proc_readscsi(inode, file, 0, -1); /* seek in it */ return(file->f_pos); default: return(-EINVAL); } } /* * Overrides for Emacs so that we almost follow Linus's tabbing style. * Emacs will notice this stuff at the end of the file and automatically * adjust the settings for this buffer only. This must remain at the end * of the file. * --------------------------------------------------------------------------- * Local variables: * c-indent-level: 4 * c-brace-imaginary-offset: 0 * c-brace-offset: -4 * c-argdecl-indent: 4 * c-label-offset: -4 * c-continued-statement-offset: 4 * c-continued-brace-offset: 0 * indent-tabs-mode: nil * tab-width: 8 * End: */ |