source: rtems/cpukit/libblock/src/bdpart-create.c @ 3d60c1b

4.115
Last change on this file since 3d60c1b was 796967c, checked in by Sebastian Huber <sebastian.huber@…>, on 02/28/12 at 16:19:49

libblock: Change bdbuf API

The functions

o rtems_bdbuf_get(),
o rtems_bdbuf_read(),
o rtems_bdbuf_syncdev(), and
o rtems_bdbuf_purge_dev(),

use now the disk device instead of the device identifier. This makes
bdbuf independent of rtems_disk_obtain() and rtems_disk_release(). It
is the responsiblity of the file system to obtain the disk device. This
also reduces the overhead to get a buffer.

The key for the AVL tree uses now the disk device instead of the device
identifier. The pointer is interpreted as an unsigned integer. This
reduces the memory overhead and makes the comparison operation a bit
faster.

Removed function rtems_bdbuf_purge_major(). This function was too
destructive and could have unpredictable side effects.

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_bdpart
5 *
6 * Block device partition management.
7 */
8
9/*
10 * Copyright (c) 2009, 2010
11 * embedded brains GmbH
12 * Obere Lagerstr. 30
13 * D-82178 Puchheim
14 * Germany
15 * <rtems@embedded-brains.de>
16 *
17 * The license and distribution terms for this file may be
18 * found in the file LICENSE in this distribution or at
19 * http://www.rtems.com/license/LICENSE.
20 */
21
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
26#include <rtems.h>
27#include <rtems/bdpart.h>
28
29rtems_status_code rtems_bdpart_create(
30  const char *disk_name,
31  const rtems_bdpart_format *format,
32  rtems_bdpart_partition *pt,
33  const unsigned *dist,
34  size_t count
35)
36{
37  rtems_status_code sc = RTEMS_SUCCESSFUL;
38  bool dos_compatibility = format != NULL
39    && format->type == RTEMS_BDPART_FORMAT_MBR
40    && format->mbr.dos_compatibility;
41  rtems_blkdev_bnum disk_end = 0;
42  rtems_blkdev_bnum pos = 0;
43  rtems_blkdev_bnum dist_sum = 0;
44  rtems_blkdev_bnum record_space =
45    dos_compatibility ? RTEMS_BDPART_MBR_CYLINDER_SIZE : 1;
46  rtems_blkdev_bnum overhead = 0;
47  rtems_blkdev_bnum free_space = 0;
48  size_t i = 0;
49
50  /* Check if we have something to do */
51  if (count == 0) {
52    /* Nothing to do */
53    return RTEMS_SUCCESSFUL;
54  }
55
56  /* Check parameter */
57  if (format == NULL || pt == NULL || dist == NULL) {
58    return RTEMS_INVALID_ADDRESS;
59  }
60
61  /* Get disk data */
62  sc = rtems_bdpart_get_disk_data( disk_name, NULL, NULL, &disk_end);
63  if (sc != RTEMS_SUCCESSFUL) {
64    return sc;
65  }
66
67  /* Get distribution sum and check for overflow */
68  for (i = 0; i < count; ++i) {
69    unsigned prev_sum = dist_sum;
70
71    dist_sum += dist [i];
72
73    if (dist_sum < prev_sum) {
74      return RTEMS_INVALID_NUMBER;
75    }
76
77    if (dist [i] == 0) {
78      return RTEMS_INVALID_NUMBER;
79    }
80  }
81
82  /* Check format */
83  if (format->type != RTEMS_BDPART_FORMAT_MBR) {
84    return RTEMS_NOT_IMPLEMENTED;
85  }
86
87  /* Align end of disk on cylinder boundary if necessary */
88  if (dos_compatibility) {
89    disk_end -= (disk_end % record_space);
90  }
91
92  /*
93   * We need at least space for the MBR and the compatibility space for the
94   * first primary partition.
95   */
96  overhead += record_space;
97
98  /*
99   * In case we need an extended partition and logical partitions we have to
100   * account for the space of each EBR.
101   */
102  if (count > 4) {
103    overhead += (count - 3) * record_space;
104  }
105
106  /*
107   * Account space to align every partition on cylinder boundaries if
108   * necessary.
109   */
110  if (dos_compatibility) {
111    overhead += (count - 1) * record_space;
112  }
113
114  /* Check disk space */
115  if ((overhead + count) > disk_end) {
116    return RTEMS_IO_ERROR;
117  }
118
119  /* Begin of first primary partition */
120  pos = record_space;
121
122  /* Space for partitions */
123  free_space = disk_end - overhead;
124
125  for (i = 0; i < count; ++i) {
126    rtems_bdpart_partition *p = pt + i;
127
128    /* Partition size */
129    rtems_blkdev_bnum s = free_space * dist [i];
130    if (s < free_space || s < dist [i]) {
131      /* TODO: Calculate without overflow */
132      return RTEMS_INVALID_NUMBER;
133    }
134    s /= dist_sum;
135
136    /* Ensure that the partition is not empty */
137    if (s == 0) {
138      s = 1;
139    }
140
141    /* Align partition upwards */
142    s += record_space - (s % record_space);
143
144    /* Reserve space for the EBR if necessary */
145    if (count > 4 && i > 2) {
146      pos += record_space;
147    }
148
149    /* Partition begin and end */
150    p->begin = pos;
151    pos += s;
152    p->end = pos;
153  }
154
155  /* Expand the last partition to the disk end */
156  pt [count - 1].end = disk_end;
157
158  return RTEMS_SUCCESSFUL;
159}
Note: See TracBrowser for help on using the repository browser.