source: rtems/cpukit/libcsupport/src/mount.c @ f1fcf71d

4.10
Last change on this file since f1fcf71d was f1fcf71d, checked in by Sebastian Huber <sebastian.huber@…>, on 07/01/10 at 14:57:08

2010-06-22 Sebastian Huber <sebastian.huber@…>

  • libcsupport/src/mount.c: Fixed string assignment in mount entry allocation.
  • Property mode set to 100644
File size: 8.5 KB
Line 
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 *
10 *  COPYRIGHT (c) 1989-1999.
11 *  On-Line Applications Research Corporation (OAR).
12 *
13 *  Copyright (c) 2010 embedded brains GmbH.
14 *
15 *  The license and distribution terms for this file may be
16 *  found in the file LICENSE in this distribution or at
17 *  http://www.rtems.com/license/LICENSE.
18 *
19 *  $Id$
20 */
21
22#if HAVE_CONFIG_H
23#include "config.h"
24#endif
25
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <rtems/chain.h>
29#include <rtems/seterr.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <errno.h>
33#include <stdlib.h>
34#include <string.h>
35#include <assert.h>
36
37#include <rtems/libio_.h>
38
39/*
40 * Mount table list.
41 */
42RTEMS_CHAIN_DEFINE_EMPTY(rtems_filesystem_mount_table_control);
43
44/*
45 * Default pathconfs.
46 */
47const rtems_filesystem_limits_and_options_t rtems_filesystem_default_pathconf = {
48   5,    /* link_max: count */
49   128,  /* max_canon: max formatted input line size */
50   7,    /* max_input: max input line size */
51   255,  /* name_max: max name */
52   255,  /* path_max: max path */
53   1024, /* pipe_buf: pipe buffer size */
54   0,    /* posix_async_io: async IO supported on fs, 0=no, 1=yes */
55   0 ,   /* posix_chown_restrictions: can chown: 0=no, 1=yes */
56   1,    /* posix_no_trunc: error on filenames > max name, 0=no, 1=yes */
57   0,    /* posix_prio_io: priority IO, 0=no, 1=yes */
58   0,    /* posix_sync_io: file can be sync'ed, 0=no, 1=yes */
59   0     /* posix_vdisable: special char processing, 0=no, 1=yes */
60};
61
62/*
63 *  Is_node_fs_root
64 *
65 *  This routine will run through the entries that currently exist in the
66 *  mount table chain. For each entry in the mount table chain it will
67 *  compare the mount tables root node to the node describing the selected
68 *  mount point. If any match is found true is returned else false is
69 *  returned.
70 *
71 */
72
73static bool Is_node_fs_root(
74  rtems_filesystem_location_info_t *loc
75)
76{
77  rtems_chain_node *node = NULL;
78
79  /*
80   * For each mount table entry
81   */
82  for ( node = rtems_chain_first( &rtems_filesystem_mount_table_control );
83        !rtems_chain_is_tail( &rtems_filesystem_mount_table_control, node );
84        node = rtems_chain_next( node ) ) {
85    rtems_filesystem_mount_table_entry_t *mount_table_entry =
86      (rtems_filesystem_mount_table_entry_t *) node;
87
88    if ( mount_table_entry->mt_fs_root.node_access == loc->node_access )
89      return true;
90  }
91
92  return false;
93}
94
95static rtems_filesystem_mount_table_entry_t *alloc_mount_table_entry(
96  const char *source_or_null,
97  const char *target_or_null,
98  const char *filesystemtype,
99  size_t *target_length_ptr
100)
101{
102  const char *target = target_or_null != NULL ? target_or_null : "/";
103  size_t filesystemtype_size = strlen( filesystemtype ) + 1;
104  size_t source_size = source_or_null != NULL ?
105    strlen( source_or_null ) + 1 : 0;
106  size_t target_length = strlen( target );
107  size_t size = sizeof( rtems_filesystem_mount_table_entry_t )
108    + filesystemtype_size + source_size + target_length + 1;
109  rtems_filesystem_mount_table_entry_t *mt_entry = calloc( 1, size );
110
111  if ( mt_entry != NULL ) {
112    char *str = (char *) mt_entry + sizeof( *mt_entry );
113
114    strcpy( str, filesystemtype );
115    mt_entry->type = str;
116    str += filesystemtype_size;
117
118    if ( source_or_null != NULL ) {
119      strcpy( str, source_or_null );
120      mt_entry->dev = str;
121      str += source_size;
122    }
123
124    strcpy( str, target );
125    mt_entry->target = str;
126  }
127
128  *target_length_ptr = target_length;
129
130  return mt_entry;
131}
132
133/*
134 *  mount
135 *
136 *  This routine will attempt to mount a new file system at the specified
137 *  mount point. A series of tests will be run to determine if any of the
138 *  following reasons exist to prevent the mount operation:
139 *
140 *      1) The file system type or options are not valid
141 *      2) No new file system root node is specified
142 *      3) The selected file system has already been mounted
143 *      4) The mount point exists with the proper permissions to allow mounting
144 *      5) The selected mount point already has a file system mounted to it
145 *
146 */
147
148int mount(
149  const char                 *source,
150  const char                 *target,
151  const char                 *filesystemtype,
152  rtems_filesystem_options_t options,
153  const void                 *data
154)
155{
156  rtems_filesystem_fsmount_me_t mount_h = NULL;
157  rtems_filesystem_location_info_t      loc;
158  rtems_filesystem_mount_table_entry_t *mt_entry = NULL;
159  rtems_filesystem_location_info_t     *loc_to_free = NULL;
160  bool has_target = target != NULL;
161  size_t target_length = 0;
162
163  /*
164   *  Are the file system options valid?
165   */
166
167  if ( options != RTEMS_FILESYSTEM_READ_ONLY &&
168       options != RTEMS_FILESYSTEM_READ_WRITE )
169    rtems_set_errno_and_return_minus_one( EINVAL );
170
171  /*
172   *  Get mount handler
173   */
174  mount_h = rtems_filesystem_get_mount_handler( filesystemtype );
175  if ( !mount_h )
176    rtems_set_errno_and_return_minus_one( EINVAL );
177
178  /*
179   * Allocate a mount table entry
180   */
181  mt_entry = alloc_mount_table_entry(
182    source,
183    target,
184    filesystemtype,
185    &target_length
186  );
187  if ( !mt_entry )
188    rtems_set_errno_and_return_minus_one( ENOMEM );
189
190  mt_entry->mt_fs_root.mt_entry = mt_entry;
191  mt_entry->options = options;
192  mt_entry->pathconf_limits_and_options = rtems_filesystem_default_pathconf;
193
194  /*
195   *  The mount_point should be a directory with read/write/execute
196   *  permissions in the existing tree.
197   */
198
199  if ( has_target ) {
200    if ( rtems_filesystem_evaluate_path(
201           target, target_length, RTEMS_LIBIO_PERMS_RWX, &loc, true ) == -1 )
202      goto cleanup_and_bail;
203
204    loc_to_free = &loc;
205
206    /*
207     * Test for node_type_h
208     */
209
210    if (!loc.ops->node_type_h) {
211      errno =  ENOTSUP;
212      goto cleanup_and_bail;
213    }
214
215    /*
216     *  Test to see if it is a directory
217     */
218
219    if ( loc.ops->node_type_h( &loc ) != RTEMS_FILESYSTEM_DIRECTORY ) {
220      errno = ENOTDIR;
221      goto cleanup_and_bail;
222    }
223
224    /*
225     *  You can only mount one file system onto a single mount point.
226     */
227
228    if ( Is_node_fs_root(  &loc ) ){
229      errno = EBUSY;
230      goto cleanup_and_bail;
231    }
232
233    /*
234     *  This must be a good mount point, so move the location information
235     *  into the allocated mount entry.  Note:  the information that
236     *  may have been allocated in loc should not be sent to freenode
237     *  until the system is unmounted.  It may be needed to correctly
238     *  traverse the tree.
239     */
240
241    mt_entry->mt_point_node.node_access = loc.node_access;
242    mt_entry->mt_point_node.handlers = loc.handlers;
243    mt_entry->mt_point_node.ops = loc.ops;
244    mt_entry->mt_point_node.mt_entry = loc.mt_entry;
245
246    /*
247     *  This link to the parent is only done when we are dealing with system
248     *  below the base file system
249     */
250
251    if ( !loc.ops->mount_h ){
252      errno = ENOTSUP;
253      goto cleanup_and_bail;
254    }
255
256    if ( loc.ops->mount_h( mt_entry ) ) {
257      goto cleanup_and_bail;
258    }
259  } else {
260    /*
261     * Do we already have a base file system ?
262     */
263    if ( !rtems_chain_is_empty( &rtems_filesystem_mount_table_control ) ) {
264      errno = EINVAL;
265      goto cleanup_and_bail;
266    }
267
268    /*
269     *  This is a mount of the base file system --> The
270     *  mt_point_node.node_access will be left to null to indicate that this
271     *  is the root of the entire file system.
272     */
273  }
274
275  if ( (*mount_h)( mt_entry, data ) ) {
276    /*
277     * Try to undo the mount operation
278     */
279    if ( loc.ops->unmount_h ) {
280      loc.ops->unmount_h( mt_entry );
281    }
282    goto cleanup_and_bail;
283  }
284
285  /*
286   *  Add the mount table entry to the mount table chain
287   */
288  rtems_chain_append( &rtems_filesystem_mount_table_control,
289                      &mt_entry->Node );
290
291  if ( !has_target )
292    rtems_filesystem_root = mt_entry->mt_fs_root;
293
294  return 0;
295
296cleanup_and_bail:
297
298  free( mt_entry );
299
300  if ( loc_to_free )
301    rtems_filesystem_freenode( loc_to_free );
302
303  return -1;
304}
305
306/*
307 * Get the first entry in the mount table.
308 */
309rtems_filesystem_mount_table_entry_t *
310rtems_filesystem_mounts_first(
311  void
312)
313{
314  rtems_filesystem_mount_table_entry_t *entry = NULL;
315
316  if ( !rtems_chain_is_empty( &rtems_filesystem_mount_table_control ) )
317    entry = (rtems_filesystem_mount_table_entry_t *)
318      rtems_chain_first( &rtems_filesystem_mount_table_control );
319
320  return entry;
321}
322
323/*
324 * Get the next entry in the mount table.
325 */
326rtems_filesystem_mount_table_entry_t *
327rtems_filesystem_mounts_next(
328  rtems_filesystem_mount_table_entry_t *entry
329)
330{
331  if ( !entry )
332    return NULL;
333  return (rtems_filesystem_mount_table_entry_t *)
334    rtems_chain_next( &entry->Node );
335}
Note: See TracBrowser for help on using the repository browser.