Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:15

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  mount()
0005  *
0006  *  Mange the mount table. You can iterate on mounts and file systems, as well
0007  *  as add and remove file systems not in the file system confiration table.
0008  *
0009  *  COPYRIGHT (c) Chris Johns <chrisj@rtems.org> 2010.
0010  *
0011  *  Copyright (c) 2010 embedded brains GmbH & Co. KG
0012  *
0013  * Redistribution and use in source and binary forms, with or without
0014  * modification, are permitted provided that the following conditions
0015  * are met:
0016  * 1. Redistributions of source code must retain the above copyright
0017  *    notice, this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright
0019  *    notice, this list of conditions and the following disclaimer in the
0020  *    documentation and/or other materials provided with the distribution.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0032  * POSSIBILITY OF SUCH DAMAGE.
0033  */
0034 
0035 #ifdef HAVE_CONFIG_H
0036 #include "config.h"
0037 #endif
0038 
0039 #include <sys/types.h>
0040 #include <sys/stat.h>
0041 #include <rtems/chain.h>
0042 #include <rtems/seterr.h>
0043 #include <fcntl.h>
0044 #include <unistd.h>
0045 #include <errno.h>
0046 #include <stdlib.h>
0047 #include <string.h>
0048 
0049 #include <rtems/libio_.h>
0050 
0051 typedef struct {
0052   rtems_chain_node node;
0053   rtems_filesystem_table_t entry;
0054 } filesystem_node;
0055 
0056 static RTEMS_CHAIN_DEFINE_EMPTY(filesystem_chain);
0057 
0058 bool rtems_filesystem_iterate(
0059   rtems_per_filesystem_routine routine,
0060   void *routine_arg
0061 )
0062 {
0063   rtems_chain_control *chain = &filesystem_chain;
0064   const rtems_filesystem_table_t *table_entry = &rtems_filesystem_table [0];
0065   rtems_chain_node *node = NULL;
0066   bool stop = false;
0067 
0068   while ( table_entry->type && !stop ) {
0069     stop = (*routine)( table_entry, routine_arg );
0070     ++table_entry;
0071   }
0072 
0073   if ( !stop ) {
0074     rtems_libio_lock();
0075     for (
0076       node = rtems_chain_first( chain );
0077       !rtems_chain_is_tail( chain, node ) && !stop;
0078       node = rtems_chain_next( node )
0079     ) {
0080       const filesystem_node *fsn = (filesystem_node *) node;
0081 
0082       stop = (*routine)( &fsn->entry, routine_arg );
0083     }
0084     rtems_libio_unlock();
0085   }
0086 
0087   return stop;
0088 }
0089 
0090 typedef struct {
0091   const char *type;
0092   rtems_filesystem_fsmount_me_t mount_h;
0093 } find_arg;
0094 
0095 static bool find_handler(const rtems_filesystem_table_t *entry, void *arg)
0096 {
0097   find_arg *fa = arg;
0098 
0099   if ( strcmp( entry->type, fa->type ) != 0 ) {
0100     return false;
0101   } else {
0102     fa->mount_h = entry->mount_h;
0103 
0104     return true;
0105   }
0106 }
0107 
0108 rtems_filesystem_fsmount_me_t
0109 rtems_filesystem_get_mount_handler(
0110   const char *type
0111 )
0112 {
0113   find_arg fa = {
0114     .type = type,
0115     .mount_h = NULL
0116   };
0117 
0118   if ( type != NULL ) {
0119     rtems_filesystem_iterate( find_handler, &fa );
0120   }
0121 
0122   return fa.mount_h;
0123 }
0124 
0125 int
0126 rtems_filesystem_register(
0127   const char                    *type,
0128   rtems_filesystem_fsmount_me_t  mount_h
0129 )
0130 {
0131   rtems_chain_control *chain = &filesystem_chain;
0132   size_t type_size = strlen(type) + 1;
0133   size_t fsn_size = sizeof( filesystem_node ) + type_size;
0134   filesystem_node *fsn = malloc( fsn_size );
0135   char *type_storage = (char *) fsn + sizeof( *fsn );
0136 
0137   if ( fsn == NULL )
0138     rtems_set_errno_and_return_minus_one( ENOMEM );
0139 
0140   memcpy(type_storage, type, type_size);
0141   fsn->entry.type = type_storage;
0142   fsn->entry.mount_h = mount_h;
0143 
0144   rtems_libio_lock();
0145   if ( rtems_filesystem_get_mount_handler( type ) == NULL ) {
0146     rtems_chain_initialize_node( &fsn->node );
0147     rtems_chain_append_unprotected( chain, &fsn->node );
0148   } else {
0149     rtems_libio_unlock();
0150     free( fsn );
0151 
0152     rtems_set_errno_and_return_minus_one( EINVAL );
0153   }
0154   rtems_libio_unlock();
0155 
0156   return 0;
0157 }
0158 
0159 int
0160 rtems_filesystem_unregister(
0161   const char *type
0162 )
0163 {
0164   rtems_chain_control *chain = &filesystem_chain;
0165   rtems_chain_node *node = NULL;
0166 
0167   if ( type == NULL ) {
0168     rtems_set_errno_and_return_minus_one( EINVAL );
0169   }
0170 
0171   rtems_libio_lock();
0172   for (
0173     node = rtems_chain_first( chain );
0174     !rtems_chain_is_tail( chain, node );
0175     node = rtems_chain_next( node )
0176   ) {
0177     filesystem_node *fsn = (filesystem_node *) node;
0178 
0179     if ( strcmp( fsn->entry.type, type ) == 0 ) {
0180       rtems_chain_extract_unprotected( node );
0181       free( fsn );
0182       rtems_libio_unlock();
0183 
0184       return 0;
0185     }
0186   }
0187   rtems_libio_unlock();
0188 
0189   rtems_set_errno_and_return_minus_one( ENOENT );
0190 }