summaryrefslogtreecommitdiffstats
path: root/cpukit/libcsupport/src/unmount.c
blob: dfd1797112037a3fe168bb1db7a975470ea760f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 *  unmount() - Unmount a File System
 *
 *  This routine is not defined in the POSIX 1003.1b standard but 
 *  in some form is supported on most UNIX and POSIX systems.  This
 *  routine is necessary to mount instantiations of a file system
 *  into the file system name space.
 *
 *  COPYRIGHT (c) 1989-1999.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 *
 *  $Id$
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <chain.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <rtems/libio_.h>

/*
 *  Data structures and routines private to mount/unmount pair.
 */

extern Chain_Control                    rtems_filesystem_mount_table_control;

int search_mt_for_mount_point(
  rtems_filesystem_location_info_t *location_of_mount_point
);


int file_systems_below_this_mountpoint( 
  const char                            *path,
  rtems_filesystem_location_info_t      *temp_loc,
  rtems_filesystem_mount_table_entry_t  *temp_mt_entry
);

/*
 *  unmount
 * 
 *  This routine will attempt to unmount the file system that has been
 *  is mounted a path.  If the operation is successful, 0 will
 *  be returned to the calling routine.  Otherwise, 1 will be returned.
 */

int unmount(
  const char *path
)
{
  int                                   status;
  rtems_filesystem_location_info_t      fs_root_loc;
  rtems_filesystem_mount_table_entry_t  temp_mt_entry;

  /*
   *  Are there any file systems below the path specified
   */

  status = file_systems_below_this_mountpoint( 
    path, 
    &fs_root_loc,
    &temp_mt_entry
  );

  if ( status != 0 )
    return -1;

  /*
   *  Is the current node reference pointing to a node in the file system
   *  we are attempting to unmount ?
   */

  if ( rtems_filesystem_current.mt_entry == fs_root_loc.mt_entry ) {
    rtems_filesystem_freenode( &fs_root_loc );
    set_errno_and_return_minus_one( EBUSY );
  }

  /*
   *  Run the file descriptor table to determine if there are any file
   *  descriptors that are currently active and reference nodes in the
   *  file system that we are trying to unmount 
   */

  if ( rtems_libio_is_open_files_in_fs( fs_root_loc.mt_entry ) == 1 ) {
    rtems_filesystem_freenode( &fs_root_loc );
    set_errno_and_return_minus_one( EBUSY );
  }
  
  /*
   * Allow the file system being unmounted on to do its cleanup.
   */

  if ((temp_mt_entry.mt_point_node.ops->unmount_h )( fs_root_loc.mt_entry ) != 0 ) {
    rtems_filesystem_freenode( &fs_root_loc );
    return -1;
  }

  /*
   * Run the unmount function for the subordinate file system.
   */

  if ((temp_mt_entry.mt_fs_root.ops->fsunmount_me_h )( fs_root_loc.mt_entry ) != 0){
    rtems_filesystem_freenode( &fs_root_loc );
    return -1;
  }

  /*
   *  Extract the mount table entry from the chain
   */

  Chain_Extract( ( Chain_Node * ) fs_root_loc.mt_entry );

  /*
   *  Free the memory associated with the extracted mount table entry.
   */

  rtems_filesystem_freenode( &fs_root_loc.mt_entry->mt_point_node );
  free( fs_root_loc.mt_entry );
  rtems_filesystem_freenode( &fs_root_loc );

  return 0;
}


/*
 *  file_systems_below_this_mountpoint
 *
 *  This routine will run through the entries that currently exist in the
 *  mount table chain. For each entry in the mount table chain it will
 *  compare the mount tables mt_fs_root to the new_fs_root_node. If any of the
 *  mount table file system root nodes matches the new file system root node
 *  this indicates that we are trying to mount a file system that has already
 *  been mounted. This is not a permitted operation. temp_loc is set to 
 *  the root node of the file system being unmounted.
 */

int file_systems_below_this_mountpoint( 
  const char                            *path,
  rtems_filesystem_location_info_t      *fs_root_loc,
  rtems_filesystem_mount_table_entry_t  *fs_to_unmount
)
{
  Chain_Node                           *the_node;
  rtems_filesystem_mount_table_entry_t *the_mount_entry;

  /*
   *  Is the path even a valid node name in the existing tree?
   */

  if ( rtems_filesystem_evaluate_path( path, 0x0, fs_root_loc, TRUE ) )
    return -1;
  
  /*
   * Verify this is the root node for the file system to be unmounted.
   */

  *fs_to_unmount = *fs_root_loc->mt_entry;
  if ( fs_to_unmount->mt_fs_root.node_access != fs_root_loc->node_access )
    set_errno_and_return_minus_one( EACCES );

  /*
   * Search the mount table for any mount entries referencing this
   * mount entry.
   */

  for ( the_node = rtems_filesystem_mount_table_control.first;
        !Chain_Is_tail( &rtems_filesystem_mount_table_control, the_node );
        the_node = the_node->next ) {
     the_mount_entry = ( rtems_filesystem_mount_table_entry_t * )the_node;
     if (the_mount_entry->mt_point_node.mt_entry  == fs_root_loc->mt_entry ) {
          set_errno_and_return_minus_one( EBUSY );
     }
  }

  return 0;
}