source: rtems/cpukit/libfs/src/rfs/rtems-rfs-rtems-file.c @ 3b7c123

4.115
Last change on this file since 3b7c123 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: 8.4 KB
Line 
1/*
2 *  COPYRIGHT (c) 2010 Chris Johns <chrisj@rtems.org>
3 *
4 *  The license and distribution terms for this file may be
5 *  found in the file LICENSE in this distribution or at
6 *  http://www.rtems.com/license/LICENSE.
7 *
8 *  $Id$
9 */
10/**
11 * @file
12 *
13 * @ingroup rtems-rfs
14 *
15 * RTEMS RFS File Handlers
16 *
17 * This file contains the set of handlers used to process operations on
18 * RFS file nodes.
19 */
20
21#if HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <inttypes.h>
26
27#if SIZEOF_OFF_T == 8
28#define PRIdoff_t PRId64
29#elif SIZEOF_OFF_T == 4
30#define PRIdoff_t PRId32
31#else
32#error "unsupported size of off_t"
33#endif
34
35#include <rtems/rfs/rtems-rfs-file.h>
36#include "rtems-rfs-rtems.h"
37
38/**
39 * This routine processes the open() system call.  Note that there is nothing
40 * special to be done at open() time.
41 */
42
43static int
44rtems_rfs_rtems_file_open (rtems_libio_t* iop,
45                           const char*    pathname,
46                           int            oflag,
47                           mode_t         mode)
48{
49  rtems_rfs_file_system* fs = rtems_rfs_rtems_pathloc_dev (&iop->pathinfo);
50  rtems_rfs_ino          ino;
51  rtems_rfs_file_handle* file;
52  int                    flags;
53  int                    rc;
54
55  flags = 0;
56
57  if (rtems_rfs_rtems_trace (RTEMS_RFS_RTEMS_DEBUG_FILE_OPEN))
58    printf("rtems-rfs: file-open: path:%s ino:%" PRId32 " flags:%04i mode:%04" PRIu32 "\n",
59           pathname, ino, flags, mode);
60
61  rtems_rfs_rtems_lock (fs);
62
63  ino = rtems_rfs_rtems_get_iop_ino (iop);
64
65  rc = rtems_rfs_file_open (fs, ino, flags, &file);
66  if (rc > 0)
67  {
68    rtems_rfs_rtems_unlock (fs);
69    return rtems_rfs_rtems_error ("file-open: open", rc);
70  }
71
72  if (rtems_rfs_rtems_trace (RTEMS_RFS_RTEMS_DEBUG_FILE_OPEN))
73    printf("rtems-rfs: file-open: handle:%p\n", file);
74
75  iop->size = rtems_rfs_file_size (file);
76  rtems_rfs_rtems_set_iop_file_handle (iop, file);
77
78  rtems_rfs_rtems_unlock (fs);
79  return 0;
80}
81
82/**
83 * This routine processes the close() system call.  Note that there is nothing
84 * to flush at this point.
85 *
86 * @param iop
87 * @return int
88 */
89static int
90rtems_rfs_rtems_file_close (rtems_libio_t* iop)
91{
92  rtems_rfs_file_handle* file = rtems_rfs_rtems_get_iop_file_handle (iop);
93  rtems_rfs_file_system* fs = rtems_rfs_file_fs (file);
94  int                    rc;
95
96  if (rtems_rfs_rtems_trace (RTEMS_RFS_RTEMS_DEBUG_FILE_CLOSE))
97    printf("rtems-rfs: file-close: handle:%p\n", file);
98
99  rtems_rfs_rtems_lock (fs);
100
101  rc = rtems_rfs_file_close (fs, file);
102  if (rc > 0)
103    rc = rtems_rfs_rtems_error ("file-close: file close", rc);
104
105  rtems_rfs_rtems_unlock (fs);
106  return rc;
107}
108
109/**
110 * This routine processes the read() system call.
111 *
112 * @param iop
113 * @param buffer
114 * @param count
115 * @return int
116 */
117static ssize_t
118rtems_rfs_rtems_file_read (rtems_libio_t* iop,
119                           void*          buffer,
120                           size_t         count)
121{
122  rtems_rfs_file_handle* file = rtems_rfs_rtems_get_iop_file_handle (iop);
123  rtems_rfs_pos          pos;
124  uint8_t*               data = buffer;
125  ssize_t                read = 0;
126  int                    rc;
127
128  if (rtems_rfs_rtems_trace (RTEMS_RFS_RTEMS_DEBUG_FILE_READ))
129    printf("rtems-rfs: file-read: handle:%p count:%zd\n", file, count);
130
131  rtems_rfs_rtems_lock (rtems_rfs_file_fs (file));
132
133  pos = iop->offset;
134
135  if (pos < rtems_rfs_file_size (file))
136  {
137    while (count)
138    {
139      size_t size;
140
141      rc = rtems_rfs_file_io_start (file, &size, true);
142      if (rc > 0)
143      {
144        read = rtems_rfs_rtems_error ("file-read: read: io-start", rc);
145        break;
146      }
147
148      if (size == 0)
149        break;
150
151      if (size > count)
152        size = count;
153
154      memcpy (data, rtems_rfs_file_data (file), size);
155
156      data  += size;
157      count -= size;
158      read  += size;
159
160      rc = rtems_rfs_file_io_end (file, size, true);
161      if (rc > 0)
162      {
163        read = rtems_rfs_rtems_error ("file-read: read: io-end", rc);
164        break;
165      }
166    }
167  }
168
169  rtems_rfs_rtems_unlock (rtems_rfs_file_fs (file));
170
171  return read;
172}
173
174/**
175 * This routine processes the write() system call.
176 *
177 * @param iop
178 * @param buffer
179 * @param count
180 * @return ssize_t
181 */
182static ssize_t
183rtems_rfs_rtems_file_write (rtems_libio_t* iop,
184                            const void*    buffer,
185                            size_t         count)
186{
187  rtems_rfs_file_handle* file = rtems_rfs_rtems_get_iop_file_handle (iop);
188  rtems_rfs_pos          pos;
189  const uint8_t*         data = buffer;
190  ssize_t                write = 0;
191  int                    rc;
192
193  if (rtems_rfs_rtems_trace (RTEMS_RFS_RTEMS_DEBUG_FILE_WRITE))
194    printf("rtems-rfs: file-write: handle:%p count:%zd\n", file, count);
195
196  rtems_rfs_rtems_lock (rtems_rfs_file_fs (file));
197
198  pos = iop->offset;
199
200  /*
201   * If the iop position is past the physical end of the file we need to set
202   * the file size to the new length before writing. If the position equals the
203   * size of file we are still past the end of the file as positions number
204   * from 0. For a specific position we need a file that has a length of one
205   * more.
206   */
207
208  if (pos >= rtems_rfs_file_size (file))
209  {
210    rc = rtems_rfs_file_set_size (file, pos + 1);
211    if (rc)
212    {
213      rtems_rfs_rtems_unlock (rtems_rfs_file_fs (file));
214      return rtems_rfs_rtems_error ("file-write: write extend", rc);
215    }
216  }
217
218  rtems_rfs_file_set_bpos (file, pos);
219
220  while (count)
221  {
222    size_t size = count;
223
224    rc = rtems_rfs_file_io_start (file, &size, false);
225    if (rc)
226    {
227      write = rtems_rfs_rtems_error ("file-write: write open", rc);
228      break;
229    }
230
231    if (size > count)
232      size = count;
233
234    memcpy (rtems_rfs_file_data (file), data, size);
235
236    data  += size;
237    count -= size;
238    write  += size;
239
240    rc = rtems_rfs_file_io_end (file, size, false);
241    if (rc)
242    {
243      write = rtems_rfs_rtems_error ("file-write: write close", rc);
244      break;
245    }
246  }
247
248  iop->size = rtems_rfs_file_size (file);
249
250  rtems_rfs_rtems_unlock (rtems_rfs_file_fs (file));
251
252  return write;
253}
254
255/**
256 * This routine processes the ioctl() system call.
257 *
258 * @note  No ioctl()'s are currently supported for RFS files.
259 *
260 * @param iop
261 * @param command
262 * @param buffer
263 */
264
265static int
266rtems_rfs_rtems_file_ioctl (rtems_libio_t* iop, uint32_t command, void* buffer)
267{
268  return 0;
269}
270
271/**
272 * This routine processes the lseek() system call.
273 *
274 * @param iop
275 * @param offset
276 * @param whence
277 * @return off_t
278 */
279static off_t
280rtems_rfs_rtems_file_lseek (rtems_libio_t* iop,
281                            off_t          offset,
282                            int            whence)
283{
284  rtems_rfs_file_handle* file = rtems_rfs_rtems_get_iop_file_handle (iop);
285  rtems_rfs_pos          pos;
286  int                    rc;
287
288  if (rtems_rfs_rtems_trace (RTEMS_RFS_RTEMS_DEBUG_FILE_LSEEK))
289    printf("rtems-rfs: file-lseek: handle:%p offset:%" PRIdoff_t "\n", file, offset);
290
291  rtems_rfs_rtems_lock (rtems_rfs_file_fs (file));
292
293  pos = iop->offset;
294
295  rc = rtems_rfs_file_seek (file, pos, &pos);
296  if (rc)
297  {
298    rtems_rfs_rtems_unlock (rtems_rfs_file_fs (file));
299    return rtems_rfs_rtems_error ("file_lseek: lseek", rc);
300  }
301
302  rtems_rfs_rtems_unlock (rtems_rfs_file_fs (file));
303
304  return iop->offset;
305}
306
307/**
308 * This routine processes the ftruncate() system call.
309 *
310 * @param iop
311 * @param length
312 * @return int
313 */
314static int
315rtems_rfs_rtems_file_ftruncate (rtems_libio_t* iop,
316                                off_t          length)
317{
318  rtems_rfs_file_handle* file = rtems_rfs_rtems_get_iop_file_handle (iop);
319  int                    rc;
320
321  if (rtems_rfs_rtems_trace (RTEMS_RFS_RTEMS_DEBUG_FILE_FTRUNC))
322    printf("rtems-rfs: file-ftrunc: handle:%p length:%" PRIdoff_t "\n", file, length);
323
324  rtems_rfs_rtems_lock (rtems_rfs_file_fs (file));
325
326  rc = rtems_rfs_file_set_size (file, length);
327  if (rc)
328    rc = rtems_rfs_rtems_error ("file_ftruncate: set size", rc);
329
330  iop->size = rtems_rfs_file_size (file);
331
332  rtems_rfs_rtems_unlock (rtems_rfs_file_fs (file));
333
334  return rc;
335}
336
337/*
338 *  Set of operations handlers for operations on RFS files.
339 */
340
341const rtems_filesystem_file_handlers_r rtems_rfs_rtems_file_handlers = {
342  .open_h      = rtems_rfs_rtems_file_open,
343  .close_h     = rtems_rfs_rtems_file_close,
344  .read_h      = rtems_rfs_rtems_file_read,
345  .write_h     = rtems_rfs_rtems_file_write,
346  .ioctl_h     = rtems_rfs_rtems_file_ioctl,
347  .lseek_h     = rtems_rfs_rtems_file_lseek,
348  .fstat_h     = rtems_rfs_rtems_fstat,
349  .ftruncate_h = rtems_rfs_rtems_file_ftruncate,
350  .fsync_h     = rtems_rfs_rtems_fdatasync,
351  .fdatasync_h = rtems_rfs_rtems_fdatasync,
352  .fcntl_h     = rtems_filesystem_default_fcntl
353};
Note: See TracBrowser for help on using the repository browser.