source: rtems/cpukit/libcsupport/src/open.c @ 4116fce6

4.115
Last change on this file since 4116fce6 was 3b7c123, checked in by Sebastian Huber <sebastian.huber@…>, on 03/13/12 at 10:33:51

Filesystem: Reference counting for locations

o A new data structure rtems_filesystem_global_location_t was

introduced to be used for

o the mount point location in the mount table entry,
o the file system root location in the mount table entry,
o the root directory location in the user environment, and
o the current directory location in the user environment.

During the path evaluation global start locations are obtained to
ensure that the current file system instance will be not unmounted in
the meantime.

o The user environment uses now reference counting and is protected

from concurrent access.

o The path evaluation process was completely rewritten and simplified.

The IMFS, RFS, NFS, and DOSFS use now a generic path evaluation
method. Recursive calls in the path evaluation have been replaced
with iteration to avoid stack overflows. Only the evaluation of
symbolic links is recursive. No dynamic memory allocations and
intermediate buffers are used in the high level path evaluation. No
global locks are held during the file system instance specific path
evaluation process.

o Recursive symbolic link evaluation is now limited by

RTEMS_FILESYSTEM_SYMLOOP_MAX. Applications can retrieve this value
via sysconf().

o The device file system (devFS) uses now no global variables and

allocation from the workspace. Node names are allocated from the
heap.

o The upper layer lseek() performs now some parameter checks.
o The upper layer ftruncate() performs now some parameter checks.
o unmask() is now restricted to the RWX flags and protected from

concurrent access.

o The fchmod_h and rmnod_h file system node handlers are now a file

system operation.

o The unlink_h operation has been removed. All nodes are now destroyed

with the rmnod_h operation.

o New lock_h, unlock_h, clonenod_h, and are_nodes_equal_h file system

operations.

o The path evaluation and file system operations are now protected by

per file system instance lock and unlock operations.

o Fix and test file descriptor duplicate in fcntl().
o New test fstests/fsnofs01.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 *  open() - POSIX 1003.1 5.3.1 - Open a File
3 *
4 *  COPYRIGHT (c) 1989-2010.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  Modifications to support reference counting in the file system are
8 *  Copyright (c) 2012 embedded brains GmbH.
9 *
10 *  The license and distribution terms for this file may be
11 *  found in the file LICENSE in this distribution or at
12 *  http://www.rtems.com/license/LICENSE.
13 *
14 *  $Id$
15 */
16
17#if HAVE_CONFIG_H
18  #include "config.h"
19#endif
20
21#include <sys/stat.h>
22#include <fcntl.h>
23#include <stdarg.h>
24#include <unistd.h>
25
26#include <rtems/libio_.h>
27
28static void create_regular_file(
29  rtems_filesystem_eval_path_context_t *ctx,
30  mode_t mode
31)
32{
33  int rv = 0;
34  const rtems_filesystem_location_info_t *currentloc =
35    rtems_filesystem_eval_path_get_currentloc( ctx );
36  const char *token = rtems_filesystem_eval_path_get_token( ctx );
37  size_t tokenlen = rtems_filesystem_eval_path_get_tokenlen( ctx );
38
39  rv = rtems_filesystem_mknod(
40    currentloc,
41    token,
42    tokenlen,
43    S_IFREG | mode,
44    0
45  );
46
47  if ( rv == 0 ) {
48    /* The mode only applies to future accesses of the newly created file */
49    rtems_filesystem_eval_path_set_flags( ctx, 0 );
50
51    rtems_filesystem_eval_path_set_path( ctx, token, tokenlen );
52    rtems_filesystem_eval_path_continue( ctx );
53  } else {
54    rtems_filesystem_eval_path_error( ctx, 0 );
55  }
56}
57
58static int do_open(
59  rtems_libio_t *iop,
60  const char *path,
61  int oflag,
62  mode_t mode
63)
64{
65  int rv = 0;
66  int fd = iop - rtems_libio_iops;
67  int rwflag = oflag + 1;
68  bool read_access = (rwflag & _FREAD) == _FREAD;
69  bool write_access = (rwflag & _FWRITE) == _FWRITE;
70  bool make = (oflag & O_CREAT) == O_CREAT;
71  bool exclusive = (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL);
72  bool truncate = (oflag & O_TRUNC) == O_TRUNC;
73  int eval_flags = RTEMS_LIBIO_FOLLOW_LINK
74    | (read_access ? RTEMS_LIBIO_PERMS_READ : 0)
75    | (write_access ? RTEMS_LIBIO_PERMS_WRITE : 0)
76    | (make ? RTEMS_LIBIO_MAKE : 0)
77    | (exclusive ?  RTEMS_LIBIO_EXCLUSIVE : 0);
78  rtems_filesystem_eval_path_context_t ctx;
79
80  rtems_filesystem_eval_path_start( &ctx, path, eval_flags );
81
82  if ( rtems_filesystem_eval_path_has_token( &ctx ) ) {
83    create_regular_file( &ctx, mode );
84  }
85
86  if ( write_access ) {
87    const rtems_filesystem_location_info_t *currentloc =
88      rtems_filesystem_eval_path_get_currentloc( &ctx );
89    rtems_filesystem_node_types_t type =
90      (*currentloc->ops->node_type_h)( currentloc );
91
92    if ( type == RTEMS_FILESYSTEM_DIRECTORY ) {
93      rtems_filesystem_eval_path_error( &ctx, EISDIR );
94    }
95  }
96
97  iop->flags |= rtems_libio_fcntl_flags( oflag );
98  rtems_filesystem_eval_path_extract_currentloc( &ctx, &iop->pathinfo );
99  rtems_filesystem_eval_path_cleanup( &ctx );
100
101  rv = (*iop->pathinfo.handlers->open_h)( iop, path, oflag, mode );
102
103  if ( rv == 0 ) {
104    if ( truncate ) {
105      rv = ftruncate( fd, 0 );
106      if ( rv != 0 ) {
107        (*iop->pathinfo.handlers->close_h)( iop );
108      }
109    }
110
111    if ( rv == 0 ) {
112      rv = fd;
113    } else {
114      rv = -1;
115    }
116  }
117
118  if ( rv < 0 ) {
119    rtems_libio_free( iop );
120  }
121
122  return rv;
123}
124
125int open( const char *path, int oflag, ... )
126{
127  int rv = 0;
128  va_list ap;
129  mode_t mode = 0;
130  rtems_libio_t *iop = NULL;
131
132  va_start( ap, oflag );
133
134  mode = va_arg( ap, mode_t );
135
136  iop = rtems_libio_allocate();
137  if ( iop != NULL ) {
138    rv = do_open( iop, path, oflag, mode );
139  } else {
140    errno = ENFILE;
141    rv = -1;
142  }
143
144  va_end( ap );
145
146  return rv;
147}
148
149/*
150 *  _open_r
151 *
152 *  This is the Newlib dependent reentrant version of open().
153 */
154
155#if defined(RTEMS_NEWLIB) && !defined(HAVE__OPEN_R)
156
157#include <reent.h>
158
159int _open_r(
160  struct _reent *ptr __attribute__((unused)),
161  const char    *buf,
162  int            oflag,
163  int            mode
164)
165{
166  return open( buf, oflag, mode );
167}
168#endif
Note: See TracBrowser for help on using the repository browser.