source: rtems/cpukit/libcsupport/src/sup_fs_location.c @ 3b7c123

4.11
Last change on this file since 3b7c123 was 3b7c123, checked in by Sebastian Huber <sebastian.huber@…>, on Mar 13, 2012 at 10:33:51 AM

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: 5.9 KB
Line 
1/*
2 * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Obere Lagerstr. 30
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
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
15#if HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#define __RTEMS_VIOLATE_KERNEL_VISIBILITY__
20
21#include <stdlib.h>
22
23#include <rtems/libio_.h>
24#include <rtems/score/thread.h>
25
26static rtems_filesystem_global_location_t *deferred_released_global_locations;
27
28rtems_filesystem_location_info_t *rtems_filesystem_location_copy(
29  rtems_filesystem_location_info_t *dst,
30  const rtems_filesystem_location_info_t *src
31)
32{
33  dst->node_access = src->node_access;
34  dst->node_access_2 = src->node_access_2;
35  dst->handlers = src->handlers;
36  dst->ops = src->ops;
37  dst->mt_entry = src->mt_entry;
38  rtems_filesystem_location_add_to_mt_entry(dst);
39
40  return dst;
41}
42
43void rtems_filesystem_location_detach(
44  rtems_filesystem_location_info_t *detach
45)
46{
47  rtems_filesystem_location_free(detach);
48  rtems_filesystem_location_initialize_to_null(detach);
49}
50
51void rtems_filesystem_location_copy_and_detach(
52  rtems_filesystem_location_info_t *copy,
53  rtems_filesystem_location_info_t *detach
54)
55{
56  rtems_filesystem_location_copy(copy, detach);
57  rtems_filesystem_location_remove_from_mt_entry(detach);
58  rtems_filesystem_location_initialize_to_null(detach);
59}
60
61rtems_filesystem_global_location_t *rtems_filesystem_location_transform_to_global(
62  rtems_filesystem_location_info_t *loc
63)
64{
65  rtems_filesystem_global_location_t *global_loc = malloc(sizeof(*global_loc));
66
67  if (global_loc != NULL) {
68    global_loc->reference_count = 1;
69    global_loc->deferred_released_next = NULL;
70    global_loc->deferred_released_count = 0;
71    rtems_filesystem_location_copy(&global_loc->location, loc);
72    rtems_filesystem_location_remove_from_mt_entry(loc);
73  } else {
74    rtems_filesystem_location_free(loc);
75    global_loc = rtems_filesystem_global_location_obtain_null();
76    errno = ENOMEM;
77  }
78
79  return global_loc;
80}
81
82void rtems_filesystem_global_location_assign(
83  rtems_filesystem_global_location_t **lhs_global_loc_ptr,
84  rtems_filesystem_global_location_t *rhs_global_loc
85)
86{
87  rtems_filesystem_mt_entry_declare_lock_context(lock_context);
88  rtems_filesystem_global_location_t *lhs_global_loc;
89
90  rtems_filesystem_mt_entry_lock(lock_context);
91  lhs_global_loc = *lhs_global_loc_ptr;
92  *lhs_global_loc_ptr = rhs_global_loc;
93  rtems_filesystem_mt_entry_unlock(lock_context);
94
95  rtems_filesystem_global_location_release(lhs_global_loc);
96}
97
98static void release_with_count(
99  rtems_filesystem_global_location_t *global_loc,
100  int count
101)
102{
103  rtems_filesystem_mount_table_entry_t *mt_entry =
104    global_loc->location.mt_entry;
105  rtems_filesystem_mt_entry_declare_lock_context(lock_context);
106  bool do_free;
107  bool do_unmount;
108
109  rtems_filesystem_mt_entry_lock(lock_context);
110  global_loc->reference_count -= count;
111  do_free = global_loc->reference_count == 0;
112  do_unmount = rtems_filesystem_is_ready_for_unmount(mt_entry);
113  rtems_filesystem_mt_entry_unlock(lock_context);
114
115  if (do_free) {
116    rtems_filesystem_location_free(&global_loc->location);
117    free(global_loc);
118  }
119
120  if (do_unmount) {
121    rtems_filesystem_do_unmount(mt_entry);
122  }
123}
124
125static void deferred_release(void)
126{
127  rtems_filesystem_global_location_t *current = NULL;
128
129  do {
130    int count = 0;
131
132    _Thread_Disable_dispatch();
133    current = deferred_released_global_locations;
134    if (current != NULL) {
135      deferred_released_global_locations = current->deferred_released_next;
136      count = current->deferred_released_count;
137      current->deferred_released_next = NULL;
138      current->deferred_released_count = 0;
139    }
140    _Thread_Enable_dispatch();
141
142    if (current != NULL) {
143      release_with_count(current, count);
144    }
145  } while (current != NULL);
146}
147
148rtems_filesystem_global_location_t *rtems_filesystem_global_location_obtain(
149  rtems_filesystem_global_location_t *const *global_loc_ptr
150)
151{
152  rtems_filesystem_mt_entry_declare_lock_context(lock_context);
153  rtems_filesystem_global_location_t *global_loc;
154
155  if (deferred_released_global_locations != NULL) {
156    deferred_release();
157  }
158
159  rtems_filesystem_mt_entry_lock(lock_context);
160  global_loc = *global_loc_ptr;
161  if (global_loc == NULL || !global_loc->location.mt_entry->mounted) {
162    global_loc = &rtems_filesystem_global_location_null;
163    errno = ENXIO;
164  }
165  ++global_loc->reference_count;
166  rtems_filesystem_mt_entry_unlock(lock_context);
167
168  return global_loc;
169}
170
171void rtems_filesystem_global_location_release(
172  rtems_filesystem_global_location_t *global_loc
173)
174{
175  if (!_Thread_Dispatch_in_critical_section()) {
176    release_with_count(global_loc, 1);
177  } else {
178    if (global_loc->deferred_released_count == 0) {
179      rtems_filesystem_global_location_t *head =
180        deferred_released_global_locations;
181
182      global_loc->deferred_released_count = 1;
183      global_loc->deferred_released_next = head;
184      deferred_released_global_locations = global_loc;
185    } else {
186      ++global_loc->deferred_released_count;
187    }
188  }
189}
190
191void rtems_filesystem_location_remove_from_mt_entry(
192  rtems_filesystem_location_info_t *loc
193)
194{
195  rtems_filesystem_mt_entry_declare_lock_context(lock_context);
196  bool do_unmount;
197
198  rtems_filesystem_mt_entry_lock(lock_context);
199  rtems_chain_extract_unprotected(&loc->mt_entry_node);
200  do_unmount = rtems_filesystem_is_ready_for_unmount(loc->mt_entry);
201  rtems_filesystem_mt_entry_unlock(lock_context);
202
203  if (do_unmount) {
204    rtems_filesystem_do_unmount(loc->mt_entry);
205  }
206}
207
208void rtems_filesystem_do_unmount(
209  rtems_filesystem_mount_table_entry_t *mt_entry
210)
211{
212  rtems_filesystem_mt_lock();
213  rtems_chain_extract_unprotected(&mt_entry->mt_node);
214  rtems_filesystem_mt_unlock();
215  rtems_filesystem_global_location_release(mt_entry->mt_point_node);
216  (*mt_entry->mt_fs_root->location.ops->fsunmount_me_h)(mt_entry);
217  free(mt_entry);
218}
Note: See TracBrowser for help on using the repository browser.