summaryrefslogtreecommitdiffstats
path: root/cpukit/libcsupport/src/mount-mgr.c
blob: 68180a699d80a5fe03b0d55fc5108868b89a4708 (plain) (blame)
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
/*
 *  mount()
 *
 *  Mange the mount table. You can iterate on mounts and file systems, as well
 *  as add and remove file systems not in the file system confiration table.
 *
 *  COPYRIGHT (c) Chris Johns <chrisj@rtems.org> 2010.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */

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

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

#include <rtems/libio_.h>

/*
 * External defined by confdefs.h or the user.
 */
extern const rtems_filesystem_table_t configuration_filesystem_table[];

/*
 * Points to a list of filesystems added at runtime.
 */
extern rtems_chain_control *rtems_filesystem_table;

/*
 * Mount table list.
 */
extern rtems_chain_control rtems_filesystem_mount_table_control;
extern bool                rtems_filesystem_mount_table_control_init;

/*
 * Get the first entry in the filesystem table.
 */
const rtems_filesystem_table_t*
rtems_filesystem_table_first(
  void
)
{
  /*
   * We can assume this because it is the root file system.
   */
  return &configuration_filesystem_table[0];
}

/*
 * Get the next entry in the file system table.
 */
const rtems_filesystem_table_t*
rtems_filesystem_table_next(
  const rtems_filesystem_table_t *entry
)
{
  const rtems_filesystem_table_t* fs;

  fs = rtems_filesystem_table_first( );
  
  while ( fs->type && ( fs != entry ) )
    ++fs;
  
  if ( fs->type ) {
    ++fs;
    if ( fs->type )
      return fs;
  }

  if ( rtems_filesystem_table ) {
    rtems_chain_node* node;
    for (node = rtems_chain_first( rtems_filesystem_table );
         !rtems_chain_is_tail( rtems_filesystem_table, node);
         node = rtems_chain_next( node )) {
      rtems_filesystem_table_node_t* tnode;
      tnode = (rtems_filesystem_table_node_t*) node;
      if ( entry == &tnode->entry ) {
        node = rtems_chain_next( node );
        if ( !rtems_chain_is_tail( rtems_filesystem_table, node ) ) {
          tnode = (rtems_filesystem_table_node_t*) node;
          return &tnode->entry;
        }
      }
    }
  }
  
  return NULL;
}

/*
 * Get the first entry in the mount table.
 */
rtems_filesystem_mount_table_entry_t*
rtems_filesystem_mounts_first(
  void
)
{
  rtems_filesystem_mount_table_entry_t* entry = NULL;
  if ( rtems_filesystem_mount_table_control_init ) {
    if ( !rtems_chain_is_empty( &rtems_filesystem_mount_table_control ) )
      entry = (rtems_filesystem_mount_table_entry_t*)
        rtems_chain_first( &rtems_filesystem_mount_table_control );
  }
  return entry;
}

/*
 * Get the next entry in the mount table.
 */
rtems_filesystem_mount_table_entry_t*
rtems_filesystem_mounts_next(
  rtems_filesystem_mount_table_entry_t *entry
)
{
  if ( !rtems_filesystem_mount_table_control_init || !entry )
    return NULL;
  return (rtems_filesystem_mount_table_entry_t*) rtems_chain_next( &entry->Node );
}

/*
 * Register a file system.
 */
int
rtems_filesystem_register(
  const char                    *type,
  rtems_filesystem_fsmount_me_t  mount_h
)
{
  rtems_filesystem_table_node_t *fs;
  if ( !rtems_filesystem_table ) {
    rtems_filesystem_table = malloc( sizeof( rtems_chain_control ) );
    if ( !rtems_filesystem_table )
      rtems_set_errno_and_return_minus_one( ENOMEM );
    rtems_chain_initialize_empty ( rtems_filesystem_table );
  }
  fs = malloc( sizeof( rtems_filesystem_table_node_t ) );
  if ( !fs )
    rtems_set_errno_and_return_minus_one( ENOMEM );
  fs->entry.type = strdup( type );
  if ( !fs->entry.type ) {
    free( fs );
    rtems_set_errno_and_return_minus_one( ENOMEM );
  }    
  fs->entry.mount_h = mount_h;
  rtems_chain_append( rtems_filesystem_table, &fs->node );
  return 0;
}

/*
 * Unregister a file system.
 */
int
rtems_filesystem_unregister(
  const char *type
)
{
  if ( rtems_filesystem_table ) {
    rtems_chain_node *node;
    for (node = rtems_chain_first( rtems_filesystem_table );
         !rtems_chain_is_tail( rtems_filesystem_table, node );
         node = rtems_chain_next( node ) ) {
      rtems_filesystem_table_node_t *fs;
      fs = (rtems_filesystem_table_node_t*) node;
      if ( strcmp( fs->entry.type, type ) == 0 ) {
        rtems_chain_extract( node );
        free( (void*) fs->entry.type );
        free( fs );
        return 0;
      }
    }
  }
  rtems_set_errno_and_return_minus_one( ENOENT );
}