1 | /*
|
---|
2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup liblabel
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file Dummy label (for disks that have no recognized label)
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <mem.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 |
|
---|
40 | #include "dummy.h"
|
---|
41 |
|
---|
42 | static int dummy_open(label_bd_t *, label_t **);
|
---|
43 | static int dummy_create(label_bd_t *, label_t **);
|
---|
44 | static void dummy_close(label_t *);
|
---|
45 | static int dummy_destroy(label_t *);
|
---|
46 | static int dummy_get_info(label_t *, label_info_t *);
|
---|
47 | static label_part_t *dummy_part_first(label_t *);
|
---|
48 | static label_part_t *dummy_part_next(label_part_t *);
|
---|
49 | static void dummy_part_get_info(label_part_t *, label_part_info_t *);
|
---|
50 | static int dummy_part_create(label_t *, label_part_spec_t *, label_part_t **);
|
---|
51 | static int dummy_part_destroy(label_part_t *);
|
---|
52 | static int dummy_suggest_ptype(label_t *, label_pcnt_t, label_ptype_t *);
|
---|
53 |
|
---|
54 | label_ops_t dummy_label_ops = {
|
---|
55 | .open = dummy_open,
|
---|
56 | .create = dummy_create,
|
---|
57 | .close = dummy_close,
|
---|
58 | .destroy = dummy_destroy,
|
---|
59 | .get_info = dummy_get_info,
|
---|
60 | .part_first = dummy_part_first,
|
---|
61 | .part_next = dummy_part_next,
|
---|
62 | .part_get_info = dummy_part_get_info,
|
---|
63 | .part_create = dummy_part_create,
|
---|
64 | .part_destroy = dummy_part_destroy,
|
---|
65 | .suggest_ptype = dummy_suggest_ptype
|
---|
66 | };
|
---|
67 |
|
---|
68 | static int dummy_open(label_bd_t *bd, label_t **rlabel)
|
---|
69 | {
|
---|
70 | label_t *label = NULL;
|
---|
71 | label_part_t *part = NULL;
|
---|
72 | size_t bsize;
|
---|
73 | aoff64_t nblocks;
|
---|
74 | uint64_t ba_min, ba_max;
|
---|
75 | int rc;
|
---|
76 |
|
---|
77 | rc = bd->ops->get_bsize(bd->arg, &bsize);
|
---|
78 | if (rc != EOK) {
|
---|
79 | rc = EIO;
|
---|
80 | goto error;
|
---|
81 | }
|
---|
82 |
|
---|
83 | rc = bd->ops->get_nblocks(bd->arg, &nblocks);
|
---|
84 | if (rc != EOK) {
|
---|
85 | rc = EIO;
|
---|
86 | goto error;
|
---|
87 | }
|
---|
88 |
|
---|
89 | label = calloc(1, sizeof(label_t));
|
---|
90 | if (label == NULL)
|
---|
91 | return ENOMEM;
|
---|
92 |
|
---|
93 | list_initialize(&label->parts);
|
---|
94 | list_initialize(&label->pri_parts);
|
---|
95 | list_initialize(&label->log_parts);
|
---|
96 |
|
---|
97 | ba_min = 0;
|
---|
98 | ba_max = nblocks;
|
---|
99 |
|
---|
100 | label->ops = &dummy_label_ops;
|
---|
101 | label->ltype = lt_none;
|
---|
102 | label->bd = *bd;
|
---|
103 | label->ablock0 = ba_min;
|
---|
104 | label->anblocks = ba_max - ba_min + 1;
|
---|
105 | label->pri_entries = 0;
|
---|
106 | label->block_size = bsize;
|
---|
107 |
|
---|
108 | part = calloc(1, sizeof(label_part_t));
|
---|
109 | if (part == NULL)
|
---|
110 | return ENOMEM;
|
---|
111 |
|
---|
112 | part->index = 0;
|
---|
113 | part->block0 = ba_min;
|
---|
114 | part->nblocks = ba_max - ba_min;
|
---|
115 | part->ptype.fmt = lptf_num;
|
---|
116 |
|
---|
117 | part->label = label;
|
---|
118 | list_append(&part->lparts, &label->parts);
|
---|
119 | list_append(&part->lpri, &label->pri_parts);
|
---|
120 |
|
---|
121 |
|
---|
122 | *rlabel = label;
|
---|
123 | return EOK;
|
---|
124 | error:
|
---|
125 | free(part);
|
---|
126 | free(label);
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 |
|
---|
130 | static int dummy_create(label_bd_t *bd, label_t **rlabel)
|
---|
131 | {
|
---|
132 | return ENOTSUP;
|
---|
133 | }
|
---|
134 |
|
---|
135 | static void dummy_close(label_t *label)
|
---|
136 | {
|
---|
137 | label_part_t *part;
|
---|
138 |
|
---|
139 | part = dummy_part_first(label);
|
---|
140 | while (part != NULL) {
|
---|
141 | list_remove(&part->lparts);
|
---|
142 | list_remove(&part->lpri);
|
---|
143 | free(part);
|
---|
144 | part = dummy_part_first(label);
|
---|
145 | }
|
---|
146 |
|
---|
147 | free(label);
|
---|
148 | }
|
---|
149 |
|
---|
150 | static int dummy_destroy(label_t *label)
|
---|
151 | {
|
---|
152 | return ENOTSUP;
|
---|
153 | }
|
---|
154 |
|
---|
155 | static int dummy_get_info(label_t *label, label_info_t *linfo)
|
---|
156 | {
|
---|
157 | memset(linfo, 0, sizeof(label_info_t));
|
---|
158 | linfo->ltype = lt_none;
|
---|
159 | linfo->flags = 0;
|
---|
160 | linfo->ablock0 = label->ablock0;
|
---|
161 | linfo->anblocks = label->anblocks;
|
---|
162 | return EOK;
|
---|
163 | }
|
---|
164 |
|
---|
165 | static label_part_t *dummy_part_first(label_t *label)
|
---|
166 | {
|
---|
167 | link_t *link;
|
---|
168 |
|
---|
169 | link = list_first(&label->parts);
|
---|
170 | if (link == NULL)
|
---|
171 | return NULL;
|
---|
172 |
|
---|
173 | return list_get_instance(link, label_part_t, lparts);
|
---|
174 | }
|
---|
175 |
|
---|
176 | static label_part_t *dummy_part_next(label_part_t *part)
|
---|
177 | {
|
---|
178 | link_t *link;
|
---|
179 |
|
---|
180 | link = list_next(&part->lparts, &part->label->parts);
|
---|
181 | if (link == NULL)
|
---|
182 | return NULL;
|
---|
183 |
|
---|
184 | return list_get_instance(link, label_part_t, lparts);
|
---|
185 | }
|
---|
186 |
|
---|
187 | static void dummy_part_get_info(label_part_t *part, label_part_info_t *pinfo)
|
---|
188 | {
|
---|
189 | pinfo->index = part->index;
|
---|
190 | pinfo->pkind = lpk_primary;
|
---|
191 | pinfo->block0 = part->block0;
|
---|
192 | pinfo->nblocks = part->nblocks;
|
---|
193 | }
|
---|
194 |
|
---|
195 | static int dummy_part_create(label_t *label, label_part_spec_t *pspec,
|
---|
196 | label_part_t **rpart)
|
---|
197 | {
|
---|
198 | return ENOTSUP;
|
---|
199 | }
|
---|
200 |
|
---|
201 | static int dummy_part_destroy(label_part_t *part)
|
---|
202 | {
|
---|
203 | return ENOTSUP;
|
---|
204 | }
|
---|
205 |
|
---|
206 | static int dummy_suggest_ptype(label_t *label, label_pcnt_t pcnt,
|
---|
207 | label_ptype_t *ptype)
|
---|
208 | {
|
---|
209 | return ENOTSUP;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /** @}
|
---|
213 | */
|
---|