source: rtems/cpukit/libfs/src/rfs/rtems-rfs-group.c @ 1d539c0

4.104.115
Last change on this file since 1d539c0 was 355b0544, checked in by Chris Johns <chrisj@…>, on 03/27/10 at 04:04:40

2010-03-27 Chris Johns <chrisj@…>

libfs/src/nfsclient/src/cexphelp.c,
libfs/src/nfsclient/src/dirutils.c,
libfs/src/nfsclient/src/nfs.modini.c,
libfs/src/nfsclient/src/nfsTest.c,
libfs/src/nfsclient/src/rpcio.c,
libfs/src/nfsclient/src/rpcio.modini.c,
libfs/src/nfsclient/src/sock_mbuf.c,
libfs/src/nfsclient/src/xdr_mbuf.c,
libfs/src/rfs/rtems-rfs-bitmaps-ut.c,
libfs/src/rfs/rtems-rfs-bitmaps.c,
libfs/src/rfs/rtems-rfs-block.c,
libfs/src/rfs/rtems-rfs-buffer-bdbuf.c,
libfs/src/rfs/rtems-rfs-buffer-devio.c,
libfs/src/rfs/rtems-rfs-buffer.c,
libfs/src/rfs/rtems-rfs-dir-hash.c, libfs/src/rfs/rtems-rfs-dir.c,
libfs/src/rfs/rtems-rfs-file-system.c,
libfs/src/rfs/rtems-rfs-file.c, libfs/src/rfs/rtems-rfs-format.c,
libfs/src/rfs/rtems-rfs-group.c, libfs/src/rfs/rtems-rfs-inode.c,
libfs/src/rfs/rtems-rfs-link.c, libfs/src/rfs/rtems-rfs-mutex.c,
libfs/src/rfs/rtems-rfs-rtems-dev.c,
libfs/src/rfs/rtems-rfs-rtems-dir.c,
libfs/src/rfs/rtems-rfs-rtems-file.c,
libfs/src/rfs/rtems-rfs-rtems-utils.c,
libfs/src/rfs/rtems-rfs-rtems.c, libfs/src/rfs/rtems-rfs-shell.c,
libfs/src/rfs/rtems-rfs-trace.c: Add HAVE_CONFIG_H support to let
files receive configure defines.

  • Property mode set to 100644
File size: 9.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 File Systems Group Routines.
16 *
17 * These functions open and close a group as well as manage bit allocations
18 * within a group.
19 */
20
21#if HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <rtems/rfs/rtems-rfs-file-system.h>
26#include <rtems/rfs/rtems-rfs-group.h>
27
28int
29rtems_rfs_group_open (rtems_rfs_file_system* fs,
30                      rtems_rfs_buffer_block base,
31                      size_t                 size,
32                      size_t                 inodes,
33                      rtems_rfs_group*       group)
34{
35  int rc;
36 
37  if (base >= rtems_rfs_fs_blocks (fs))
38  {
39    if (rtems_rfs_trace (RTEMS_RFS_TRACE_GROUP_OPEN))
40      printf ("rtems-rfs: group-open: base outside file system range: %d: %s\n",
41              EIO, strerror (EIO));
42    return EIO;
43  }
44
45  if ((base + size) >= rtems_rfs_fs_blocks (fs))
46    size = rtems_rfs_fs_blocks (fs) - base;
47 
48  if (rtems_rfs_trace (RTEMS_RFS_TRACE_GROUP_OPEN))
49    printf ("rtems-rfs: group-open: base=%ld, blocks=%zd inodes=%zd\n",
50            base, size, inodes);
51
52  group->base = base;
53  group->size = size;
54 
55  rc = rtems_rfs_buffer_handle_open (fs, &group->block_bitmap_buffer); 
56  if (rc > 0)
57  {
58    if (rtems_rfs_trace (RTEMS_RFS_TRACE_GROUP_OPEN))
59      printf ("rtems-rfs: group-open: could not open block bitmap handle: %d: %s\n",
60              rc, strerror (rc));
61    return rc;
62  }
63
64  rc = rtems_rfs_bitmap_open (&group->block_bitmap, fs,
65                              &group->block_bitmap_buffer, size,
66                              group->base + RTEMS_RFS_GROUP_BLOCK_BITMAP_BLOCK);
67  if (rc > 0)
68  {
69    rtems_rfs_buffer_handle_close (fs, &group->block_bitmap_buffer);
70    if (rtems_rfs_trace (RTEMS_RFS_TRACE_GROUP_OPEN))
71      printf ("rtems-rfs: group-open: could not open block bitmap: %d: %s\n",
72              rc, strerror (rc));
73    return rc;
74  }
75
76  rc = rtems_rfs_buffer_handle_open (fs, &group->inode_bitmap_buffer);
77  if (rc > 0)
78  {
79    rtems_rfs_bitmap_close (&group->block_bitmap);
80    rtems_rfs_buffer_handle_close (fs, &group->block_bitmap_buffer);
81    if (rtems_rfs_trace (RTEMS_RFS_TRACE_GROUP_OPEN))
82      printf ("rtems-rfs: group-open: could not open inode bitmap handle: %d: %s\n",
83              rc, strerror (rc));
84    return rc;
85  }
86
87  rc = rtems_rfs_bitmap_open (&group->inode_bitmap, fs,
88                              &group->inode_bitmap_buffer, inodes,
89                              group->base + RTEMS_RFS_GROUP_INODE_BITMAP_BLOCK);
90  if (rc > 0)
91  {
92    rtems_rfs_buffer_handle_close (fs, &group->inode_bitmap_buffer);
93    rtems_rfs_bitmap_close (&group->block_bitmap);
94    rtems_rfs_buffer_handle_close (fs, &group->block_bitmap_buffer);
95    if (rtems_rfs_trace (RTEMS_RFS_TRACE_GROUP_OPEN))
96      printf ("rtems-rfs: group-open: could not open inode bitmap: %d: %s\n",
97              rc, strerror (rc));
98    return rc;
99  }
100
101  if (rtems_rfs_fs_release_bitmaps (fs))
102  {
103    rtems_rfs_bitmap_release_buffer (fs, &group->block_bitmap);
104    rtems_rfs_bitmap_release_buffer (fs, &group->inode_bitmap);
105  }
106 
107  return 0;
108}
109
110int
111rtems_rfs_group_close (rtems_rfs_file_system* fs, rtems_rfs_group* group)
112{
113  int result = 0;
114  int rc;
115
116  if (rtems_rfs_trace (RTEMS_RFS_TRACE_GROUP_CLOSE))
117    printf ("rtems-rfs: group-close: base=%ld\n", group->base);
118
119  /*
120   * We need to close as much as possible and also return any error if one
121   * occurs but this may result in one even more important error being lost but
122   * we cannot OR the errors together so this is a reasonable compromise.
123   */
124  rc = rtems_rfs_bitmap_close (&group->inode_bitmap);
125  if (rc > 0)
126    result = rc;
127  rc = rtems_rfs_buffer_handle_close (fs, &group->inode_bitmap_buffer);
128  if (rc > 0)
129    result = rc;
130  rc = rtems_rfs_bitmap_close (&group->block_bitmap);
131  if (rc > 0)
132    result = rc;
133  rc = rtems_rfs_buffer_handle_close (fs, &group->block_bitmap_buffer);
134  if (rc > 0)
135    result = rc;
136 
137  return result;
138}
139
140int
141rtems_rfs_group_bitmap_alloc (rtems_rfs_file_system* fs,
142                              rtems_rfs_bitmap_bit   goal,
143                              bool                   inode,
144                              rtems_rfs_bitmap_bit*  result)
145{
146  int                  group_start;
147  size_t               size;
148  rtems_rfs_bitmap_bit bit;
149  int                  offset;
150  bool                 updown;
151  int                  direction;
152
153  if (inode)
154  {
155    size = fs->group_inodes;
156    goal -= RTEMS_RFS_ROOT_INO;
157  }
158  else
159    size = fs->group_blocks;
160   
161  group_start = goal / size;
162  bit = (rtems_rfs_bitmap_bit) (goal % size);
163  offset = 0;
164  updown = true;
165  direction = 1;
166     
167  /*
168   * Try the goal group first and if that group fails try the groups either
169   * side until the whole file system has be tried.
170   */
171  while (true)
172  {
173    rtems_rfs_bitmap_control* bitmap;
174    int                       group;
175    bool                      allocated = false;
176    int                       rc;
177
178    /*
179     * We can start at any location and we move out from that point in each
180     * direction. The offset grows until we find a free bit or we hit an end.
181     */
182    group = group_start + (direction * offset);
183    if (offset)
184      bit = direction > 0 ? 0 : size - 1;
185   
186    /*
187     * If we are still looking up and down and if the group is out of range we
188     * have reached one end. Stopping looking up and down and just move in the
189     * one direction one group at a time.
190     */
191    if ((group < 0) || (group >= fs->group_count))
192    {
193      if (!updown)
194        break;
195      direction = direction > 0 ? -1 : 1;
196      updown = false;
197      continue;
198    }
199
200   if (inode)
201      bitmap = &fs->groups[group].inode_bitmap;
202    else
203      bitmap = &fs->groups[group].block_bitmap;
204   
205    rc = rtems_rfs_bitmap_map_alloc (bitmap, bit, &allocated, &bit);
206    if (rc > 0)
207      return rc;
208   
209    if (rtems_rfs_fs_release_bitmaps (fs))
210      rtems_rfs_bitmap_release_buffer (fs, bitmap);
211     
212    if (allocated)
213    {
214      if (inode)
215        *result = rtems_rfs_group_inode (fs, group, bit);
216      else
217        *result = rtems_rfs_group_block (&fs->groups[group], bit);
218      if (rtems_rfs_trace (RTEMS_RFS_TRACE_GROUP_BITMAPS))
219        printf ("rtems-rfs: group-bitmap-alloc: %s allocated: %ld\n",
220                inode ? "inode" : "block", *result);
221      return 0;
222    }
223
224    if (updown)
225      direction = direction > 0 ? -1 : 1;
226
227    offset++;
228  }
229 
230  if (rtems_rfs_trace (RTEMS_RFS_TRACE_GROUP_BITMAPS))
231    printf ("rtems-rfs: group-bitmap-alloc: no blocks available\n");
232
233  return ENOSPC;
234}
235
236int
237rtems_rfs_group_bitmap_free (rtems_rfs_file_system* fs,
238                             bool                   inode,
239                             rtems_rfs_bitmap_bit   no)
240{
241  rtems_rfs_bitmap_control* bitmap;
242  unsigned int              group;
243  rtems_rfs_bitmap_bit      bit;
244  size_t                    size;
245  int                       rc;
246
247  if (rtems_rfs_trace (RTEMS_RFS_TRACE_GROUP_BITMAPS))
248    printf ("rtems-rfs: group-bitmap-free: %s free: %ld\n",
249            inode ? "inode" : "block", no);
250
251  if (inode)
252  {
253    no -= RTEMS_RFS_ROOT_INO;
254    size = fs->group_inodes;
255  }   
256  else
257  {
258    no -= RTEMS_RFS_SUPERBLOCK_SIZE;
259    size = fs->group_blocks;
260  }
261 
262  group = no / size;
263  bit = (rtems_rfs_bitmap_bit) (no % size);
264 
265  if (inode)
266    bitmap = &fs->groups[group].inode_bitmap;
267  else
268    bitmap = &fs->groups[group].block_bitmap;
269
270  rc = rtems_rfs_bitmap_map_clear (bitmap, bit);
271 
272  rtems_rfs_bitmap_release_buffer (fs, bitmap);
273 
274  return rc;
275}
276
277int
278rtems_rfs_group_bitmap_test (rtems_rfs_file_system* fs,
279                             bool                   inode,
280                             rtems_rfs_bitmap_bit   no,
281                             bool*                  state)
282{
283  rtems_rfs_bitmap_control* bitmap;
284  unsigned int              group;
285  rtems_rfs_bitmap_bit      bit;
286  size_t                    size;
287  int                       rc;
288
289  if (rtems_rfs_trace (RTEMS_RFS_TRACE_GROUP_BITMAPS))
290    printf ("rtems-rfs: group-bitmap-test: %s test: %ld\n",
291            inode ? "inode" : "block", no);
292
293  if (inode)
294  {
295    if ((no < RTEMS_RFS_ROOT_INO) || (no > rtems_rfs_fs_inodes (fs)))
296        return EINVAL;
297    no -= RTEMS_RFS_ROOT_INO;
298    size = fs->group_inodes;
299  }   
300  else
301  {
302    if (no >= rtems_rfs_fs_blocks (fs))
303        return EINVAL;
304    size = fs->group_blocks;
305  }
306 
307  group = no / size;
308  bit = (rtems_rfs_bitmap_bit) (no % size);
309 
310  if (inode)
311    bitmap = &fs->groups[group].inode_bitmap;
312  else
313    bitmap = &fs->groups[group].block_bitmap;
314
315  rc = rtems_rfs_bitmap_map_test (bitmap, bit, state);
316 
317  rtems_rfs_bitmap_release_buffer (fs, bitmap);
318 
319  return rc;
320}
321
322int
323rtems_rfs_group_usage (rtems_rfs_file_system* fs,
324                       size_t*                blocks,
325                       size_t*                inodes)
326{
327  int g;
328 
329  *blocks = 0;
330  *inodes = 0;
331 
332  for (g = 0; g < fs->group_count; g++)
333  {
334    rtems_rfs_group* group = &fs->groups[g];
335    *blocks +=
336      rtems_rfs_bitmap_map_size(&group->block_bitmap) -
337      rtems_rfs_bitmap_map_free (&group->block_bitmap);
338    *inodes +=
339      rtems_rfs_bitmap_map_size (&group->inode_bitmap) -
340      rtems_rfs_bitmap_map_free (&group->inode_bitmap);
341  }
342
343  if (*blocks > rtems_rfs_fs_blocks (fs))
344    *blocks = rtems_rfs_fs_blocks (fs);
345  if (*inodes > rtems_rfs_fs_inodes (fs))
346    *inodes = rtems_rfs_fs_inodes (fs);
347 
348  return 0;
349}
350
Note: See TracBrowser for help on using the repository browser.