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  *  @file
0005  *
0006  *  @brief Unmount a File System
0007  *  @ingroup libcsupport
0008  */
0009 
0010 /*
0011  *  COPYRIGHT (c) 1989-2010.
0012  *  On-Line Applications Research Corporation (OAR).
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifdef HAVE_CONFIG_H
0037 #include "config.h"
0038 #endif
0039 
0040 #include <errno.h>
0041 
0042 #include <rtems/libio_.h>
0043 
0044 static bool contains_root_or_current_directory(
0045   const rtems_filesystem_mount_table_entry_t *mt_entry
0046 )
0047 {
0048   const rtems_filesystem_location_info_t *root =
0049     &rtems_filesystem_root->location;
0050   const rtems_filesystem_location_info_t *current =
0051     &rtems_filesystem_current->location;
0052 
0053   return mt_entry == root->mt_entry || mt_entry == current->mt_entry;
0054 }
0055 
0056 /**
0057  *  This routine is not defined in the POSIX 1003.1b standard but
0058  *  in some form is supported on most UNIX and POSIX systems.  This
0059  *  routine is necessary to mount instantiations of a file system
0060  *  into the file system name space.
0061  */
0062 int unmount( const char *path )
0063 {
0064   int rv = 0;
0065   rtems_filesystem_eval_path_context_t ctx;
0066   int eval_flags = RTEMS_FS_FOLLOW_LINK;
0067   const rtems_filesystem_location_info_t *currentloc =
0068     rtems_filesystem_eval_path_start( &ctx, path, eval_flags );
0069   rtems_filesystem_mount_table_entry_t *mt_entry = currentloc->mt_entry;
0070 
0071   if ( rtems_filesystem_location_is_instance_root( currentloc ) ) {
0072     if ( !contains_root_or_current_directory( mt_entry ) ) {
0073       const rtems_filesystem_operations_table *mt_point_ops =
0074         mt_entry->mt_point_node->location.mt_entry->ops;
0075 
0076       rv = (*mt_point_ops->unmount_h)( mt_entry );
0077       if ( rv == 0 ) {
0078         rtems_id self_task_id = rtems_task_self();
0079         rtems_filesystem_mt_entry_declare_lock_context( lock_context );
0080 
0081         rtems_filesystem_mt_entry_lock( lock_context );
0082         mt_entry->unmount_task = self_task_id;
0083         mt_entry->mounted = false;
0084         rtems_filesystem_mt_entry_unlock( lock_context );
0085       }
0086     } else {
0087       errno = EBUSY;
0088       rv = -1;
0089     }
0090   } else {
0091     errno = EACCES;
0092     rv = -1;
0093   }
0094 
0095   rtems_filesystem_eval_path_cleanup( &ctx );
0096 
0097   if ( rv == 0 ) {
0098     rtems_status_code sc = rtems_event_transient_receive(
0099       RTEMS_WAIT,
0100       RTEMS_NO_TIMEOUT
0101     );
0102 
0103     if ( sc != RTEMS_SUCCESSFUL ) {
0104       rtems_fatal_error_occurred( 0xdeadbeef );
0105     }
0106   }
0107 
0108   return rv;
0109 }