Kernel - Implementing infrastructure for GetNodeFromINode
[tpg/acess2.git] / Kernel / vfs / mount.c
1 /* 
2  * Acess Micro - VFS Server version 1
3  */
4 #include <acess.h>
5 #include <vfs.h>
6 #include <vfs_int.h>
7 #include <fs_sysfs.h>
8
9 // === IMPORTS ===
10 extern int      giVFS_MountFileID;
11 extern char     *gsVFS_MountFile;
12
13 // === PROTOTYPES ===
14 #if 0
15  int    VFS_Mount(const char *Device, const char *MountPoint, const char *Filesystem, const char *Options);
16 #endif
17 void    VFS_UpdateMountFile(void);
18
19 // === GLOBALS ===
20 tMutex  glVFS_MountList;
21 tVFS_Mount      *gVFS_Mounts;
22 tVFS_Mount      *gVFS_RootMount = NULL;
23 Uint32  giVFS_NextMountIdent = 0;
24
25 // === CODE ===
26 /**
27  * \brief Mount a device
28  * \param Device        Device string to mount
29  * \param MountPoint    Destination for the mount
30  * \param Filesystem    Filesystem to use for the mount
31  * \param Options               Options to be passed to the filesystem
32  * \return -1 on Invalid FS, -2 on No Mem, 0 on success
33  * 
34  * Mounts the filesystem on \a Device at \a MountPoint using the driver
35  * \a Filesystem. The options in the string \a Options is passed to the
36  * driver's mount.
37  */
38 int VFS_Mount(const char *Device, const char *MountPoint, const char *Filesystem, const char *Options)
39 {
40         tVFS_Mount      *mnt;
41         tVFS_Driver     *fs;
42          int    deviceLen = strlen(Device);
43          int    mountLen = strlen(MountPoint);
44          int    argLen = strlen(Options);
45         
46         // Get the filesystem
47         fs = VFS_GetFSByName(Filesystem);
48         if(!fs) {
49                 Log_Warning("VFS", "VFS_Mount - Unknown FS Type '%s'", Filesystem);
50                 return -1;
51         }
52         
53         // Create mount information
54         mnt = malloc( sizeof(tVFS_Mount)+deviceLen+1+mountLen+1+argLen+1 );
55         if(!mnt) {
56                 return -2;
57         }
58         
59         // HACK: Forces VFS_ParsePath to fall back on root  
60         if(mountLen == 1 && MountPoint[0] == '/')
61                 mnt->MountPointLen = 0;
62         else
63                 mnt->MountPointLen = mountLen;
64         
65         // Fill Structure
66         mnt->Filesystem = fs;
67         
68         mnt->Device = &mnt->StrData[0];
69         memcpy( mnt->Device, Device, deviceLen+1 );
70         
71         mnt->MountPoint = &mnt->StrData[deviceLen+1];
72         memcpy( mnt->MountPoint, MountPoint, mountLen+1 );
73         
74         mnt->Options = &mnt->StrData[deviceLen+1+mountLen+1];
75         memcpy( mnt->Options, Options, argLen+1 );
76         
77         // Initialise Volume
78         mnt->RootNode = fs->InitDevice(Device, NULL);   //&ArgString);
79         if(!mnt->RootNode) {
80                 free(mnt);
81                 return -2;
82         }
83
84         mnt->Identifier = giVFS_NextMountIdent++;
85         
86         // Set root
87         if(!gVFS_RootMount)     gVFS_RootMount = mnt;
88         
89         // Add to mount list
90         Mutex_Acquire( &glVFS_MountList );
91         {
92                 tVFS_Mount      *tmp;
93                 mnt->Next = NULL;
94                 if(gVFS_Mounts) {
95                         for( tmp = gVFS_Mounts; tmp->Next; tmp = tmp->Next );
96                         tmp->Next = mnt;
97                 }
98                 else {
99                         gVFS_Mounts = mnt;
100                 }
101         }
102         Mutex_Release( &glVFS_MountList );
103         
104         Log_Log("VFS", "Mounted '%s' to '%s' ('%s')", Device, MountPoint, Filesystem);
105         
106         VFS_UpdateMountFile();
107         
108         return 0;
109 }
110
111 /**
112  * \brief Gets a mount point given the identifier
113  */
114 tVFS_Mount *VFS_GetMountByIdent(Uint32 MountID)
115 {
116         tVFS_Mount      *mnt;
117         for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
118         {
119                 if(mnt->Identifier == MountID)
120                         return mnt;
121         }
122         return NULL;
123 }
124
125 /**
126  * \brief Updates the mount file buffer
127  * 
128  * Updates the ProcFS mounts file buffer to match the current mounts list.
129  */
130 void VFS_UpdateMountFile(void)
131 {
132          int    len = 0;
133         char    *buf;
134         tVFS_Mount      *mnt;
135         
136         // Format:
137         // <device>\t<location>\t<type>\t<options>\n
138         
139         for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
140         {
141                 len += 4 + strlen(mnt->Device) + strlen(mnt->MountPoint)
142                         + strlen(mnt->Filesystem->Name) + strlen(mnt->Options);
143         }
144         
145         buf = malloc( len + 1 );
146         len = 0;
147         for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
148         {
149                 strcpy( &buf[len], mnt->Device );
150                 len += strlen(mnt->Device);
151                 buf[len++] = '\t';
152                 
153                 strcpy( &buf[len], mnt->MountPoint );
154                 len += strlen(mnt->MountPoint);
155                 buf[len++] = '\t';
156                 
157                 strcpy( &buf[len], mnt->Filesystem->Name );
158                 len += strlen(mnt->Filesystem->Name);
159                 buf[len++] = '\t';
160                 
161                 strcpy( &buf[len], mnt->Options );
162                 len += strlen(mnt->Options);
163                 buf[len++] = '\n';
164         }
165         buf[len] = 0;
166         
167         SysFS_UpdateFile( giVFS_MountFileID, buf, len );
168         if( gsVFS_MountFile )   free( gsVFS_MountFile );
169         gsVFS_MountFile = buf;
170 }

UCC git Repository :: git.ucc.asn.au