Changeset 854

Show
Ignore:
Timestamp:
08/21/08 21:46:43 (3 years ago)
Author:
mwhitworth
Message:

Convert devfs to use new kfs layer.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • Whitix/trunk/fs/devfs/devfs.c

    r705 r854  
    3333#include <init.h> 
    3434#include <error.h> 
     35#include <fs/kfs.h> 
    3536#include <print.h> 
    3637 
     
    3839#include "../devices/storage/ata/ata.h" 
    3940 
    40 /* Local prototypes */ 
    41 int DevFsDirSize(struct DevFsDir* dir); 
    42  
    4341/* Root directory of the device filesystem. */ 
    44 struct DevFsDir* rootDir; 
    45 struct DevFsEntry rootEntry; 
     42struct KeFsDir* rootDir; 
     43struct KeFsEntry rootEntry; 
    4644 
    4745struct SuperBlockOps devFsSbOps; 
     
    127125int DevFsReadVNode(struct VNode* vNode) 
    128126{ 
    129         struct DevFsEntry* entry=(struct DevFsEntry*)(vNode->id); 
     127        struct KeFsEntry* entry=(struct KeFsEntry*)(vNode->id); 
     128        struct DevFsDevice* device; 
    130129        struct StorageDevice* sDevice; 
    131  
    132         vNode->extraInfo=entry; 
     130         
    133131        vNode->vNodeOps=&devFsVOps; 
    134  
    135132        vNode->mode=VFS_ATTR_READ | VFS_ATTR_WRITE; 
    136133 
     
    138135        { 
    139136                vNode->mode |= VFS_ATTR_FILE; 
    140                 vNode->devId.major=entry->device.devId.major; 
    141                 vNode->devId.minor=entry->device.devId.minor; 
     137                device=(struct DevFsDevice*)entry->file; 
     138                vNode->devId.major=device->devId.major; 
     139                vNode->devId.minor=device->devId.minor; 
    142140                if (entry->type & DEVICE_BLOCK) 
    143141                { 
     
    152150                        vNode->mode |= VFS_ATTR_CHAR; 
    153151                        /* Just redirect to the usual char devices */ 
    154                         vNode->fileOps=entry->device.ops; 
     152                        vNode->fileOps=device->ops; 
    155153                        vNode->size=0; 
    156154                } 
     
    159157                vNode->fileOps=&devFsFileOps; 
    160158                vNode->mode|=VFS_ATTR_DIR; 
    161                 vNode->size=DevFsDirSize(&entry->dir); 
    162         } 
    163  
    164         return 0; 
    165 } 
    166  
    167 int DevFsFreeVNode(struct VNode* vNode) 
    168 { 
    169         /* This function is so that extraInfo (which contains the DevFsEntry and could be 
    170         malloc'ed) is not freed by the virtual filesystem layer. */ 
    171         return 0; 
    172 } 
    173  
    174 int DevFsLookup(struct VNode** retVal,struct VNode* dir,char* name,int nameLength) 
    175 { 
    176         struct DevFsDir* dDir; 
    177         struct DevFsEntry* entry; 
    178  
    179         entry=(struct DevFsEntry*)(dir->extraInfo); 
    180         dDir=&entry->dir; 
    181  
    182         /* Handle '..'. '..' on the root directory will already have been handled by the VFS. */ 
    183         if (nameLength == 2 && name[0] == '.' && name[1] == '.') 
    184         { 
    185                 dDir=dDir->parent; 
    186                 entry=(struct DevFsEntry*)(((DWORD)dDir)-offsetof(struct DevFsEntry, dir)); 
    187                 goto found; 
    188         } 
    189  
    190         ListForEachEntry(entry,&(dDir->entries),next) 
    191                 if (!strnicmp(entry->name,name,nameLength)) 
    192                         goto found; 
    193  
    194         /* Cycled through the whole directory with no luck */ 
    195         return -ENOENT; 
    196  
    197 found: 
    198         *retVal=VNodeGet(dir->superBlock,(DWORD)entry); 
    199         return 0; 
    200 } 
    201  
    202 struct DevFsDir* DevDirLookup(char* name,int nameLength,struct DevFsDir* dir) 
    203 { 
    204         struct DevFsEntry* entry; 
    205          
    206         /* Look it up */ 
    207         ListForEachEntry(entry,&(dir->entries),next) 
    208         { 
    209                 if ((entry->type & VFS_ATTR_DIR) && !strnicmp(entry->name, name, nameLength)) 
    210                         return &entry->dir; 
    211         }        
    212          
    213         return NULL; 
     159                vNode->size=KeFsDirSize(&entry->dir); 
     160        } 
     161 
     162        return 0; 
    214163} 
    215164 
    216165int DevAddDevice(char* name, WORD major, WORD minor, int type, void* ops) 
    217166{ 
    218         struct DevFsEntry* entry; 
     167        struct KeFsDir* dir=rootDir; 
     168        struct KeFsEntry* entry; 
    219169        struct DevFsDevice* device; 
    220         struct DevFsDir* dir=rootDir; 
    221         char* endName; 
    222         DWORD offset; 
    223170 
    224171        /* Sanity checks */ 
     
    228175        if (!major) 
    229176                return -EINVAL; 
    230  
    231         /* Get to the directory first. */ 
    232         while ((endName=strchr(name,'/'))) 
    233         { 
    234                 offset=(DWORD)(endName-name); 
    235                 dir=DevDirLookup(name, offset,dir); 
    236                 if (!dir) 
    237                         return -ENOENT; 
    238                          
    239                 name+=offset+1; 
    240         } 
    241  
    242 //      printf("DevAddDevice(%s)\n", name); 
    243  
    244         /* Check if the name exists already */ 
    245         ListForEachEntry(entry,&(dir->entries),next) 
    246                 if (!strnicmp(entry->name,name,strlen(name))) 
    247                         return -EEXIST; 
    248  
    249         /* Allocate the DevFsEntry, and the name with it */ 
    250         entry=(struct DevFsEntry*)malloc(sizeof(struct DevFsEntry)+strlen(name)+1); 
     177         
     178        entry=KeFsAddEntry(dir, name); 
     179         
    251180        if (!entry) 
    252                 return -ENOMEM; 
    253  
    254         /* Always a device */ 
    255         entry->type=type | VFS_ATTR_FILE | VFS_ATTR_READ | VFS_ATTR_WRITE; 
    256         strcpy(entry->name,name); 
    257          
    258         device=(struct DevFsDevice*)(&entry->device); 
     181                return -EFAULT; 
     182 
     183        device=(struct DevFsDevice*)malloc(sizeof(struct DevFsDevice)); 
    259184         
    260185        device->ops=ops; 
    261186        device->devId.major=major; 
    262187        device->devId.minor=minor; 
    263         ListAddTail(&entry->next,&dir->entries); 
     188        entry->file=device; 
     189         
     190        entry->type |= type; 
    264191 
    265192        return 0; 
     
    274201{ 
    275202        /* Any storage devices will be found in the Storage directory. */ 
    276         struct DevFsDir* dir; 
    277         struct DevFsEntry* entry; 
    278          
    279         dir=DevDirLookup("Storage", strlen("Storage"), rootDir); 
     203        struct KeFsDir* dir; 
     204        struct KeFsEntry* entry; 
     205        struct DevFsDevice* device; 
     206         
     207        dir=KeFsDirLookup("Storage", strlen("Storage"), rootDir); 
    280208         
    281209        if (!dir) 
    282210                return NULL; 
    283211         
    284         ListForEachEntry(entry,&(dir->entries), next) 
     212        ListForEachEntry(entry,&dir->entries, next) 
    285213        { 
    286214                if (entry->type & VFS_ATTR_FILE) 
    287215                { 
    288                         if (entry->device.devId.major == major && entry->device.devId.minor == minor && (entry->type & DEVICE_BLOCK)) 
    289                                 return (struct StorageDevice*)(entry->device.ops); 
     216                        device=(struct DevFsDevice*)(entry->file); 
     217                        if (device->devId.major == major && device->devId.minor == minor && (entry->type & DEVICE_BLOCK)) 
     218                                return (struct StorageDevice*)(device->ops); 
    290219                } 
    291220        } 
     
    330259        else 
    331260                /* Decode root filesystem here */ 
    332                 rootDev=DevFindRootDev(rootDevMajor,rootDevMinor); 
     261                rootDev=DevFindRootDev(rootDevMajor, rootDevMinor); 
    333262 
    334263        if (!rootDev) 
     
    347276{ 
    348277        char* endName; 
    349         DWORD offset; 
    350         struct DevFsDir* dir=rootDir, *newDir; 
    351         struct DevFsEntry* entry;        
     278        DWORD offset;    
     279        struct KeFsDir* dir=rootDir; 
    352280         
    353281        /* Get to the directory */ 
     
    355283        { 
    356284                offset=(DWORD)(endName-name); 
    357                 dir=DevDirLookup(name, strlen(name),dir); 
     285                dir=KeFsDirLookup(name, strlen(name),dir); 
    358286                if (!dir) 
    359287                        return -ENOENT; 
    360288        } 
    361289         
    362         /* Check it doesn't exist already. */ 
    363         ListForEachEntry(entry,&(dir->entries),next) 
    364                 if (!strnicmp(entry->name,name,strlen(name))) 
    365                         return -EEXIST; 
    366          
    367         /* Add entry. */ 
    368         entry=(struct DevFsEntry*)malloc(sizeof(struct DevFsEntry)+strlen(name)+1); 
    369         entry->type=VFS_ATTR_DIR | VFS_ATTR_READ; 
    370         strcpy(entry->name, name); 
    371          
    372         newDir=&entry->dir; 
    373         INIT_LIST_HEAD(&newDir->entries); 
    374         newDir->parent=dir; 
    375          
    376         ListAddTail(&entry->next,&dir->entries); 
     290        KeFsAddDir(dir, name); 
     291         
    377292        return 0; 
    378293} 
     
    384299} 
    385300 
    386 int DevFsPermission(struct VNode* node,int access) 
    387 { 
    388         KePrint("DevFsPermission\n"); 
    389         return 0; 
    390 } 
    391  
    392 int DevFsReadDir(struct File* file,void* dirEntries) 
    393 { 
    394         struct DevFsEntry* entry; 
    395         struct DevFsEntry* entryDir=(struct DevFsEntry*)(file->vNode->extraInfo); 
    396         struct DevFsDir* dir=&entryDir->dir; 
    397         int ret; 
    398  
    399         ListForEachEntry(entry, &(dir->entries), next) 
    400         { 
    401                 ret=FillDir(dirEntries,entry->name,strlen(entry->name),(DWORD)entry); 
    402                 if (ret < 0) 
    403                         return ret; 
    404  
    405                 file->position++; 
    406         } 
    407  
     301int DevFsReadDir(struct File* file, void* dirEntries) 
     302{ 
     303        return KeFsReadDir(file, FillDir, dirEntries); 
     304} 
     305 
     306int DevFsLookup(struct VNode** retVal,struct VNode* dir,char* name,int nameLength) 
     307{ 
     308        struct KeFsEntry* entry; 
     309         
     310        entry=KeFsLookup(dir, name, nameLength); 
     311         
     312        *retVal=VNodeGet(dir->superBlock, (DWORD)entry); 
     313         
    408314        return 0; 
    409315} 
     
    412318{ 
    413319        .readVNode = DevFsReadVNode, 
    414         .freeVNode = DevFsFreeVNode, 
    415320}; 
    416321 
     
    418323{ 
    419324        .lookup=DevFsLookup, 
    420         .permission=DevFsPermission, 
    421325}; 
    422326 
     
    426330        .readDir=DevFsReadDir, 
    427331}; 
    428  
    429 int DevFsDirSize(struct DevFsDir* dir) 
    430 { 
    431         struct DevFsEntry* entry; 
    432         int i=0; 
    433  
    434         ListForEachEntry(entry, &(dir->entries), next) 
    435                 i++; 
    436  
    437         return i; 
    438 } 
    439332 
    440333struct VfsSuperBlock* DevFsReadSuper(struct StorageDevice* dev,int flags,char* data) 
     
    452345        retVal->sbOps=&devFsSbOps; 
    453346        retVal->mount=VNodeGet(retVal,(DWORD)&rootEntry); 
     347         
    454348        retVal->mount->mode=VFS_ATTR_DIR | VFS_ATTR_READ; 
    455         retVal->mount->size=DevFsDirSize(rootDir); 
     349        retVal->mount->size=KeFsDirSize(rootDir); 
    456350        retVal->mount->vNodeOps=&devFsVOps; 
    457351        retVal->mount->fileOps=&devFsFileOps; 
    458         retVal->mount->extraInfo=(void*)&rootEntry; 
    459352 
    460353        return retVal; 
     
    469362int DevFsInit() 
    470363{ 
    471         /* Set up the data structure here, and DevFsReadSuper doesn't do too much (at the moment) */ 
    472364        VfsRegisterFileSystem(&devFileSystem); 
    473365 
    474366        /* Set up the root directory here, as devices will be added before ReadSuper is called. */ 
    475         rootEntry.type=VFS_ATTR_DIR | VFS_ATTR_READ | VFS_ATTR_WRITE; 
    476         rootEntry.name[0]='\0'; 
    477         rootEntry.dir.parent=NULL; /* Parent is handled by VFS for the root directory. */ 
    478         INIT_LIST_HEAD(&rootEntry.dir.entries); 
     367        KeFsInitRoot(&rootEntry); 
    479368        rootDir=&rootEntry.dir; 
    480369