source: rtems/cpukit/libcsupport/src/mount.c @ 71c012af

4.115
Last change on this file since 71c012af was 71c012af, checked in by Joel Sherrill <joel.sherrill@…>, on 07/04/10 at 14:53:47

2010-07-04 Joel Sherrill <joel.sherrill@…>

  • libblock/include/rtems/ide_part_table.h, libblock/src/bdbuf.c, libcsupport/src/times.c, libcsupport/src/libio_init.c, libcsupport/src/mallocfreespace.c, libcsupport/src/mount-mgr.c, libcsupport/src/mount.c, libcsupport/src/rewinddir.c, libcsupport/src/seekdir.c, libcsupport/src/telldir.c, libcsupport/src/unmount.c, libfs/src/dosfs/fat.c, libfs/src/dosfs/fat_fat_operations.c, libfs/src/dosfs/msdos_create.c, libfs/src/dosfs/msdos_dir.c, libfs/src/dosfs/msdos_eval.c, libfs/src/dosfs/msdos_file.c, libfs/src/dosfs/msdos_format.c, libfs/src/dosfs/msdos_fsunmount.c, libfs/src/dosfs/msdos_initsupp.c, libfs/src/dosfs/msdos_rename.c, libmisc/cpuuse/cpuusagereport.c, libmisc/shell/vis.c, libmisc/stackchk/check.c, sapi/src/posixapi.c, telnetd/telnetd.c: Remove include of assert.h when it is not needed.
  • Property mode set to 100644
File size: 7.3 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-2010.
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
36#include <rtems/libio_.h>
37
38static RTEMS_CHAIN_DEFINE_EMPTY(mount_chain);
39
40/*
41 * Default pathconfs.
42 */
43const rtems_filesystem_limits_and_options_t rtems_filesystem_default_pathconf = {
44   5,    /* link_max: count */
45   128,  /* max_canon: max formatted input line size */
46   7,    /* max_input: max input line size */
47   255,  /* name_max: max name */
48   255,  /* path_max: max path */
49   1024, /* pipe_buf: pipe buffer size */
50   0,    /* posix_async_io: async IO supported on fs, 0=no, 1=yes */
51   0 ,   /* posix_chown_restrictions: can chown: 0=no, 1=yes */
52   1,    /* posix_no_trunc: error on filenames > max name, 0=no, 1=yes */
53   0,    /* posix_prio_io: priority IO, 0=no, 1=yes */
54   0,    /* posix_sync_io: file can be sync'ed, 0=no, 1=yes */
55   0     /* posix_vdisable: special char processing, 0=no, 1=yes */
56};
57
58static bool is_node_fs_root(
59  const rtems_filesystem_mount_table_entry_t *mt_entry,
60  void *arg
61)
62{
63  return arg == mt_entry->mt_fs_root.node_access;
64}
65
66static rtems_filesystem_mount_table_entry_t *alloc_mount_table_entry(
67  const char *source_or_null,
68  const char *target_or_null,
69  const char *filesystemtype,
70  size_t *target_length_ptr
71)
72{
73  const char *target = target_or_null != NULL ? target_or_null : "/";
74  size_t filesystemtype_size = strlen( filesystemtype ) + 1;
75  size_t source_size = source_or_null != NULL ?
76    strlen( source_or_null ) + 1 : 0;
77  size_t target_length = strlen( target );
78  size_t size = sizeof( rtems_filesystem_mount_table_entry_t )
79    + filesystemtype_size + source_size + target_length + 1;
80  rtems_filesystem_mount_table_entry_t *mt_entry = calloc( 1, size );
81
82  if ( mt_entry != NULL ) {
83    char *str = (char *) mt_entry + sizeof( *mt_entry );
84
85    strcpy( str, filesystemtype );
86    mt_entry->type = str;
87    str += filesystemtype_size;
88
89    if ( source_or_null != NULL ) {
90      strcpy( str, source_or_null );
91      mt_entry->dev = str;
92      str += source_size;
93    }
94
95    strcpy( str, target );
96    mt_entry->target = str;
97  }
98
99  *target_length_ptr = target_length;
100
101  return mt_entry;
102}
103
104/*
105 *  mount
106 *
107 *  This routine will attempt to mount a new file system at the specified
108 *  mount point. A series of tests will be run to determine if any of the
109 *  following reasons exist to prevent the mount operation:
110 *
111 *      1) The file system type or options are not valid
112 *      2) No new file system root node is specified
113 *      3) The selected file system has already been mounted
114 *      4) The mount point exists with the proper permissions to allow mounting
115 *      5) The selected mount point already has a file system mounted to it
116 *
117 */
118
119int mount(
120  const char                 *source,
121  const char                 *target,
122  const char                 *filesystemtype,
123  rtems_filesystem_options_t options,
124  const void                 *data
125)
126{
127  rtems_filesystem_fsmount_me_t mount_h = NULL;
128  rtems_filesystem_location_info_t      loc;
129  rtems_filesystem_mount_table_entry_t *mt_entry = NULL;
130  rtems_filesystem_location_info_t     *loc_to_free = NULL;
131  bool has_target = target != NULL;
132  size_t target_length = 0;
133
134  /*
135   *  Are the file system options valid?
136   */
137
138  if ( options != RTEMS_FILESYSTEM_READ_ONLY &&
139       options != RTEMS_FILESYSTEM_READ_WRITE )
140    rtems_set_errno_and_return_minus_one( EINVAL );
141
142  /*
143   *  Get mount handler
144   */
145  mount_h = rtems_filesystem_get_mount_handler( filesystemtype );
146  if ( !mount_h )
147    rtems_set_errno_and_return_minus_one( EINVAL );
148
149  /*
150   * Allocate a mount table entry
151   */
152  mt_entry = alloc_mount_table_entry(
153    source,
154    target,
155    filesystemtype,
156    &target_length
157  );
158  if ( !mt_entry )
159    rtems_set_errno_and_return_minus_one( ENOMEM );
160
161  mt_entry->mt_fs_root.mt_entry = mt_entry;
162  mt_entry->options = options;
163  mt_entry->pathconf_limits_and_options = rtems_filesystem_default_pathconf;
164
165  /*
166   *  The mount_point should be a directory with read/write/execute
167   *  permissions in the existing tree.
168   */
169
170  if ( has_target ) {
171    if ( rtems_filesystem_evaluate_path(
172           target, target_length, RTEMS_LIBIO_PERMS_RWX, &loc, true ) == -1 )
173      goto cleanup_and_bail;
174
175    loc_to_free = &loc;
176
177    /*
178     *  Test to see if it is a directory
179     */
180
181    if ( loc.ops->node_type_h( &loc ) != RTEMS_FILESYSTEM_DIRECTORY ) {
182      errno = ENOTDIR;
183      goto cleanup_and_bail;
184    }
185
186    /*
187     *  You can only mount one file system onto a single mount point.
188     */
189
190    if ( rtems_filesystem_mount_iterate( is_node_fs_root, loc.node_access ) ) {
191      errno = EBUSY;
192      goto cleanup_and_bail;
193    }
194
195    /*
196     *  This must be a good mount point, so move the location information
197     *  into the allocated mount entry.  Note:  the information that
198     *  may have been allocated in loc should not be sent to freenode
199     *  until the system is unmounted.  It may be needed to correctly
200     *  traverse the tree.
201     */
202
203    mt_entry->mt_point_node.node_access = loc.node_access;
204    mt_entry->mt_point_node.handlers = loc.handlers;
205    mt_entry->mt_point_node.ops = loc.ops;
206    mt_entry->mt_point_node.mt_entry = loc.mt_entry;
207
208    /*
209     *  This link to the parent is only done when we are dealing with system
210     *  below the base file system
211     */
212
213    if ( loc.ops->mount_h( mt_entry ) ) {
214      goto cleanup_and_bail;
215    }
216  } else {
217    /*
218     * Do we already have a base file system ?
219     */
220    if ( !rtems_chain_is_empty( &mount_chain ) ) {
221      errno = EINVAL;
222      goto cleanup_and_bail;
223    }
224
225    /*
226     *  This is a mount of the base file system --> The
227     *  mt_point_node.node_access will be left to null to indicate that this
228     *  is the root of the entire file system.
229     */
230  }
231
232  if ( (*mount_h)( mt_entry, data ) ) {
233    /*
234     * Try to undo the mount operation
235     */
236    loc.ops->unmount_h( mt_entry );
237    goto cleanup_and_bail;
238  }
239
240  /*
241   *  Add the mount table entry to the mount table chain
242   */
243  rtems_libio_lock();
244  rtems_chain_append( &mount_chain, &mt_entry->Node );
245  rtems_libio_unlock();
246
247  if ( !has_target )
248    rtems_filesystem_root = mt_entry->mt_fs_root;
249
250  return 0;
251
252cleanup_and_bail:
253
254  free( mt_entry );
255
256  if ( loc_to_free )
257    rtems_filesystem_freenode( loc_to_free );
258
259  return -1;
260}
261
262bool rtems_filesystem_mount_iterate(
263  rtems_per_filesystem_mount_routine routine,
264  void *routine_arg
265)
266{
267  rtems_chain_node *node = NULL;
268  bool stop = false;
269
270  rtems_libio_lock();
271  for (
272    node = rtems_chain_first( &mount_chain );
273    !rtems_chain_is_tail( &mount_chain, node ) && !stop;
274    node = rtems_chain_next( node )
275  ) {
276    const rtems_filesystem_mount_table_entry_t *mt_entry =
277      (rtems_filesystem_mount_table_entry_t *) node;
278
279    stop = (*routine)( mt_entry, routine_arg );
280  }
281  rtems_libio_unlock();
282
283  return stop;
284}
Note: See TracBrowser for help on using the repository browser.