[07a3253d] | 1 | /* |
---|
| 2 | * mount() |
---|
| 3 | * |
---|
| 4 | * XXX |
---|
| 5 | * |
---|
| 6 | * XXX make sure no required ops are NULL |
---|
| 7 | * XXX make sure no optional ops you are using are NULL |
---|
| 8 | * XXX unmount should be required. |
---|
| 9 | * |
---|
[08311cc3] | 10 | * COPYRIGHT (c) 1989-1999. |
---|
[07a3253d] | 11 | * On-Line Applications Research Corporation (OAR). |
---|
| 12 | * |
---|
| 13 | * The license and distribution terms for this file may be |
---|
| 14 | * found in the file LICENSE in this distribution or at |
---|
| 15 | * http://www.OARcorp.com/rtems/license.html. |
---|
| 16 | * |
---|
| 17 | * $Id$ |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | #include <sys/types.h> |
---|
| 21 | #include <sys/stat.h> |
---|
| 22 | #include <chain.h> |
---|
| 23 | #include <fcntl.h> |
---|
| 24 | #include <unistd.h> |
---|
| 25 | #include <errno.h> |
---|
| 26 | #include <stdlib.h> |
---|
| 27 | #include <string.h> |
---|
| 28 | #include <assert.h> |
---|
| 29 | |
---|
[3ba74c73] | 30 | #include <rtems/libio_.h> |
---|
[07a3253d] | 31 | |
---|
| 32 | Chain_Control rtems_filesystem_mount_table_control; |
---|
| 33 | |
---|
| 34 | /* |
---|
[7b0001f7] | 35 | * Prototypes that probably should be somewhere else. |
---|
[07a3253d] | 36 | */ |
---|
| 37 | |
---|
| 38 | int init_fs_mount_table( void ); |
---|
[4b0d1ab] | 39 | static int Is_node_fs_root( |
---|
| 40 | rtems_filesystem_location_info_t *loc |
---|
| 41 | ); |
---|
[07a3253d] | 42 | |
---|
| 43 | |
---|
| 44 | /* |
---|
| 45 | * XXX |
---|
| 46 | */ |
---|
| 47 | |
---|
| 48 | #define FOUND 0 |
---|
| 49 | #define NOT_FOUND -1 |
---|
| 50 | |
---|
| 51 | /* |
---|
| 52 | * mount |
---|
| 53 | * |
---|
| 54 | * This routine will attempt to mount a new file system at the specified |
---|
| 55 | * mount point. A series of tests will be run to determine if any of the |
---|
| 56 | * following reasons exist to prevent the mount operation: |
---|
| 57 | * |
---|
| 58 | * 1) The file system type or options are not valid |
---|
| 59 | * 2) No new file system root node is specified |
---|
| 60 | * 3) The selected file system has already been mounted |
---|
| 61 | * 4) The mount point exists with the proper permissions to allow mounting |
---|
| 62 | * 5) The selected mount point already has a file system mounted to it |
---|
| 63 | * |
---|
| 64 | */ |
---|
| 65 | |
---|
| 66 | int mount( |
---|
| 67 | rtems_filesystem_mount_table_entry_t **mt_entry, |
---|
| 68 | rtems_filesystem_operations_table *fs_ops, |
---|
[d71fcab] | 69 | rtems_filesystem_options_t options, |
---|
[07a3253d] | 70 | char *device, |
---|
| 71 | char *mount_point |
---|
| 72 | ) |
---|
| 73 | { |
---|
[d71fcab] | 74 | rtems_filesystem_location_info_t loc; |
---|
[07a3253d] | 75 | rtems_filesystem_mount_table_entry_t *temp_mt_entry; |
---|
[04df848] | 76 | rtems_filesystem_location_info_t *loc_to_free = NULL; |
---|
[07a3253d] | 77 | |
---|
| 78 | /* XXX add code to check for required operations */ |
---|
| 79 | |
---|
| 80 | /* |
---|
[2f87c843] | 81 | * Is there a file system operations table? |
---|
[07a3253d] | 82 | */ |
---|
| 83 | |
---|
[2f87c843] | 84 | if ( fs_ops == NULL ) { |
---|
[07a3253d] | 85 | errno = EINVAL; |
---|
| 86 | return -1; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /* |
---|
[2f87c843] | 90 | * Are the file system options valid? |
---|
[07a3253d] | 91 | */ |
---|
| 92 | |
---|
[d71fcab] | 93 | if ( options != RTEMS_FILESYSTEM_READ_ONLY && |
---|
| 94 | options != RTEMS_FILESYSTEM_READ_WRITE ) { |
---|
[07a3253d] | 95 | errno = EINVAL; |
---|
| 96 | return -1; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /* |
---|
| 100 | * Allocate a mount table entry |
---|
| 101 | */ |
---|
| 102 | |
---|
| 103 | temp_mt_entry = malloc( sizeof(rtems_filesystem_mount_table_entry_t) ); |
---|
| 104 | |
---|
[2f87c843] | 105 | if ( !temp_mt_entry ) { |
---|
| 106 | errno = ENOMEM; |
---|
| 107 | return -1; |
---|
| 108 | } |
---|
| 109 | |
---|
[07a3253d] | 110 | temp_mt_entry->mt_fs_root.mt_entry = temp_mt_entry; |
---|
| 111 | temp_mt_entry->options = options; |
---|
[2f87c843] | 112 | if ( device ) |
---|
[556ea0e3] | 113 | strcpy( temp_mt_entry->dev, device ); |
---|
[07a3253d] | 114 | else |
---|
[2f87c843] | 115 | temp_mt_entry->dev = 0; |
---|
[07a3253d] | 116 | |
---|
| 117 | /* |
---|
| 118 | * The mount_point should be a directory with read/write/execute |
---|
| 119 | * permissions in the existing tree. |
---|
| 120 | */ |
---|
| 121 | |
---|
| 122 | if ( mount_point ) { |
---|
[04df848] | 123 | |
---|
| 124 | if ( rtems_filesystem_evaluate_path( |
---|
| 125 | mount_point, RTEMS_LIBIO_PERMS_RWX, &loc, TRUE ) == -1 ) |
---|
| 126 | goto cleanup_and_bail; |
---|
| 127 | |
---|
| 128 | /* |
---|
| 129 | * Test to see if it is a directory |
---|
| 130 | */ |
---|
| 131 | |
---|
| 132 | loc_to_free = &loc; |
---|
[9c3fa30] | 133 | if ( loc.ops->node_type_h( &loc ) != RTEMS_FILESYSTEM_DIRECTORY ) { |
---|
[04df848] | 134 | errno = ENOTDIR; |
---|
| 135 | goto cleanup_and_bail; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /* |
---|
| 139 | * You can only mount one file system onto a single mount point. |
---|
| 140 | */ |
---|
| 141 | |
---|
[4b0d1ab] | 142 | if ( Is_node_fs_root( &loc ) ){ |
---|
[04df848] | 143 | errno = EBUSY; |
---|
| 144 | goto cleanup_and_bail; |
---|
| 145 | } |
---|
[07a3253d] | 146 | |
---|
[04df848] | 147 | /* |
---|
| 148 | * This must be a good mount point, so move the location information |
---|
| 149 | * into the allocated mount entry. Note: the information that |
---|
| 150 | * may have been allocated in loc should not be sent to freenode |
---|
| 151 | * until the system is unmounted. It may be needed to correctly |
---|
| 152 | * traverse the tree. |
---|
| 153 | */ |
---|
| 154 | |
---|
| 155 | temp_mt_entry->mt_point_node.node_access = loc.node_access; |
---|
| 156 | temp_mt_entry->mt_point_node.handlers = loc.handlers; |
---|
| 157 | temp_mt_entry->mt_point_node.ops = loc.ops; |
---|
| 158 | temp_mt_entry->mt_point_node.mt_entry = loc.mt_entry; |
---|
| 159 | |
---|
| 160 | /* |
---|
| 161 | * This link to the parent is only done when we are dealing with system |
---|
| 162 | * below the base file system |
---|
| 163 | */ |
---|
| 164 | |
---|
[9c3fa30] | 165 | if ( !loc.ops->mount_h ){ |
---|
[04df848] | 166 | errno = ENOTSUP; |
---|
| 167 | goto cleanup_and_bail; |
---|
| 168 | } |
---|
| 169 | |
---|
[9c3fa30] | 170 | if ( loc.ops->mount_h( temp_mt_entry ) ) { |
---|
[04df848] | 171 | goto cleanup_and_bail; |
---|
| 172 | } |
---|
| 173 | } else { |
---|
| 174 | |
---|
| 175 | /* |
---|
| 176 | * This is a mount of the base file system --> The |
---|
| 177 | * mt_point_node.node_access will be set to null to indicate that this |
---|
| 178 | * is the root of the entire file system. |
---|
| 179 | */ |
---|
| 180 | |
---|
| 181 | temp_mt_entry->mt_fs_root.node_access = NULL; |
---|
| 182 | temp_mt_entry->mt_fs_root.handlers = NULL; |
---|
| 183 | temp_mt_entry->mt_fs_root.ops = NULL; |
---|
| 184 | |
---|
| 185 | temp_mt_entry->mt_point_node.node_access = NULL; |
---|
| 186 | temp_mt_entry->mt_point_node.handlers = NULL; |
---|
| 187 | temp_mt_entry->mt_point_node.ops = NULL; |
---|
| 188 | temp_mt_entry->mt_point_node.mt_entry = NULL; |
---|
[07a3253d] | 189 | } |
---|
| 190 | |
---|
[9c3fa30] | 191 | if ( !fs_ops->fsmount_me_h ) { |
---|
[04df848] | 192 | errno = ENOTSUP; |
---|
| 193 | goto cleanup_and_bail; |
---|
[07a3253d] | 194 | } |
---|
| 195 | |
---|
[9c3fa30] | 196 | if ( fs_ops->fsmount_me_h( temp_mt_entry ) ) |
---|
[04df848] | 197 | goto cleanup_and_bail; |
---|
[07a3253d] | 198 | |
---|
| 199 | /* |
---|
[04df848] | 200 | * Add the mount table entry to the mount table chain |
---|
[07a3253d] | 201 | */ |
---|
| 202 | |
---|
| 203 | Chain_Append( &rtems_filesystem_mount_table_control, &temp_mt_entry->Node ); |
---|
| 204 | |
---|
| 205 | *mt_entry = temp_mt_entry; |
---|
[d71fcab] | 206 | |
---|
[07a3253d] | 207 | return 0; |
---|
| 208 | |
---|
| 209 | cleanup_and_bail: |
---|
| 210 | |
---|
| 211 | free( temp_mt_entry ); |
---|
[d71fcab] | 212 | |
---|
[04df848] | 213 | if ( loc_to_free ) |
---|
| 214 | rtems_filesystem_freenode( loc_to_free ); |
---|
| 215 | |
---|
[07a3253d] | 216 | return -1; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | /* |
---|
| 222 | * init_fs_mount_table |
---|
| 223 | * |
---|
| 224 | * This routine will initialize the chain control element that manages the |
---|
| 225 | * mount table chain. |
---|
| 226 | */ |
---|
| 227 | |
---|
| 228 | int init_fs_mount_table() |
---|
| 229 | { |
---|
| 230 | Chain_Initialize_empty ( &rtems_filesystem_mount_table_control ); |
---|
| 231 | return 0; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | /* |
---|
[4b0d1ab] | 235 | * Is_node_fs_root |
---|
[07a3253d] | 236 | * |
---|
| 237 | * This routine will run through the entries that currently exist in the |
---|
| 238 | * mount table chain. For each entry in the mount table chain it will |
---|
[4b0d1ab] | 239 | * compare the mount tables root node to the node describing the selected |
---|
| 240 | * mount point. If any match is found true is returned else false is |
---|
| 241 | * returned. |
---|
| 242 | * |
---|
[07a3253d] | 243 | */ |
---|
| 244 | |
---|
[4b0d1ab] | 245 | static int Is_node_fs_root( |
---|
| 246 | rtems_filesystem_location_info_t *loc |
---|
[07a3253d] | 247 | ) |
---|
| 248 | { |
---|
[2f87c843] | 249 | Chain_Node *the_node; |
---|
[07a3253d] | 250 | rtems_filesystem_mount_table_entry_t *the_mount_entry; |
---|
| 251 | |
---|
[4b0d1ab] | 252 | /* |
---|
| 253 | * For each mount table entry |
---|
| 254 | */ |
---|
| 255 | |
---|
[07a3253d] | 256 | for ( the_node = rtems_filesystem_mount_table_control.first; |
---|
| 257 | !Chain_Is_tail( &rtems_filesystem_mount_table_control, the_node ); |
---|
| 258 | the_node = the_node->next ) { |
---|
| 259 | the_mount_entry = (rtems_filesystem_mount_table_entry_t *) the_node; |
---|
[4b0d1ab] | 260 | if ( the_mount_entry->mt_fs_root.node_access == loc->node_access ) |
---|
| 261 | return TRUE; |
---|
[07a3253d] | 262 | } |
---|
[4b0d1ab] | 263 | return FALSE; |
---|
[07a3253d] | 264 | } |
---|