Tizen 2.1 base
[external/device-mapper.git] / lib / metadata / pv.c
1 /*
2  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3  * Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved.
4  *
5  * This file is part of LVM2.
6  *
7  * This copyrighted material is made available to anyone wishing to use,
8  * modify, copy, or redistribute it subject to the terms and conditions
9  * of the GNU Lesser General Public License v.2.1.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this program; if not, write to the Free Software Foundation,
13  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14  */
15
16 #include "lib.h"
17 #include "metadata.h"
18 #include "lvmcache.h"
19
20 /*
21  * FIXME: Check for valid handle before dereferencing field or log error?
22  */
23 #define pv_field(handle, field) ((handle)->field)
24
25 char *pv_fmt_dup(const struct physical_volume *pv)
26 {
27         if (!pv->fmt)
28                 return NULL;
29         return dm_pool_strdup(pv->vg->vgmem, pv->fmt->name);
30 }
31
32 char *pv_name_dup(const struct physical_volume *pv)
33 {
34         return dm_pool_strdup(pv->vg->vgmem, dev_name(pv->dev));
35 }
36
37 /*
38  * Gets/Sets for external LVM library
39  */
40 struct id pv_id(const struct physical_volume *pv)
41 {
42         return pv_field(pv, id);
43 }
44
45 char *pv_uuid_dup(const struct physical_volume *pv)
46 {
47         return id_format_and_copy(pv->vg->vgmem, &pv->id);
48 }
49
50 char *pv_tags_dup(const struct physical_volume *pv)
51 {
52         return tags_format_and_copy(pv->vg->vgmem, &pv->tags);
53 }
54
55 const struct format_type *pv_format_type(const struct physical_volume *pv)
56 {
57         return pv_field(pv, fmt);
58 }
59
60 struct id pv_vgid(const struct physical_volume *pv)
61 {
62         return pv_field(pv, vgid);
63 }
64
65 struct device *pv_dev(const struct physical_volume *pv)
66 {
67         return pv_field(pv, dev);
68 }
69
70 const char *pv_vg_name(const struct physical_volume *pv)
71 {
72         return pv_field(pv, vg_name);
73 }
74
75 const char *pv_dev_name(const struct physical_volume *pv)
76 {
77         return dev_name(pv_dev(pv));
78 }
79
80 uint64_t pv_size(const struct physical_volume *pv)
81 {
82         return pv_field(pv, size);
83 }
84
85 uint64_t pv_dev_size(const struct physical_volume *pv)
86 {
87         uint64_t size;
88
89         if (!dev_get_size(pv->dev, &size))
90                 size = 0;
91         return size;
92 }
93
94 uint64_t pv_size_field(const struct physical_volume *pv)
95 {
96         uint64_t size;
97
98         if (!pv->pe_count)
99                 size = pv->size;
100         else
101                 size = (uint64_t) pv->pe_count * pv->pe_size;
102         return size;
103 }
104
105 uint64_t pv_free(const struct physical_volume *pv)
106 {
107         uint64_t freespace;
108
109         if (!pv->pe_count)
110                 freespace = pv->size;
111         else
112                 freespace = (uint64_t)
113                         (pv->pe_count - pv->pe_alloc_count) * pv->pe_size;
114         return freespace;
115 }
116
117 uint64_t pv_status(const struct physical_volume *pv)
118 {
119         return pv_field(pv, status);
120 }
121
122 uint32_t pv_pe_size(const struct physical_volume *pv)
123 {
124         return pv_field(pv, pe_size);
125 }
126
127 uint64_t pv_pe_start(const struct physical_volume *pv)
128 {
129         return pv_field(pv, pe_start);
130 }
131
132 uint32_t pv_pe_count(const struct physical_volume *pv)
133 {
134         return pv_field(pv, pe_count);
135 }
136
137 uint32_t pv_pe_alloc_count(const struct physical_volume *pv)
138 {
139         return pv_field(pv, pe_alloc_count);
140 }
141
142 uint32_t pv_mda_count(const struct physical_volume *pv)
143 {
144         struct lvmcache_info *info;
145
146         info = info_from_pvid((const char *)&pv->id.uuid, 0);
147         return info ? dm_list_size(&info->mdas) : UINT64_C(0);
148 }
149
150 uint32_t pv_mda_used_count(const struct physical_volume *pv)
151 {
152         struct lvmcache_info *info;
153         struct metadata_area *mda;
154         uint32_t used_count=0;
155
156         info = info_from_pvid((const char *)&pv->id.uuid, 0);
157         if (!info)
158                 return 0;
159         dm_list_iterate_items(mda, &info->mdas) {
160                 if (!mda_is_ignored(mda))
161                         used_count++;
162         }
163         return used_count;
164 }
165
166 /**
167  * is_orphan - Determine whether a pv is an orphan based on its vg_name
168  * @pv: handle to the physical volume
169  */
170 int is_orphan(const struct physical_volume *pv)
171 {
172         return is_orphan_vg(pv_field(pv, vg_name));
173 }
174
175 /**
176  * is_pv - Determine whether a pv is a real pv or dummy one
177  * @pv: handle to device
178  */
179 int is_pv(const struct physical_volume *pv)
180 {
181         return (pv_field(pv, vg_name) ? 1 : 0);
182 }
183
184 int is_missing_pv(const struct physical_volume *pv)
185 {
186         return pv_field(pv, status) & MISSING_PV ? 1 : 0;
187 }
188
189 char *pv_attr_dup(struct dm_pool *mem, const struct physical_volume *pv)
190 {
191         char *repstr;
192
193         if (!(repstr = dm_pool_zalloc(mem, 3))) {
194                 log_error("dm_pool_alloc failed");
195                 return NULL;
196         }
197
198         repstr[0] = (pv->status & ALLOCATABLE_PV) ? 'a' : '-';
199         repstr[1] = (pv->status & EXPORTED_VG) ? 'x' : '-';
200         return repstr;
201 }
202
203 uint64_t pv_mda_size(const struct physical_volume *pv)
204 {
205         struct lvmcache_info *info;
206         uint64_t min_mda_size = 0;
207         const char *pvid = (const char *)(&pv->id.uuid);
208
209         /* PVs could have 2 mdas of different sizes (rounding effect) */
210         if ((info = info_from_pvid(pvid, 0)))
211                 min_mda_size = find_min_mda_size(&info->mdas);
212         return min_mda_size;
213 }
214
215 uint64_t pv_mda_free(const struct physical_volume *pv)
216 {
217         struct lvmcache_info *info;
218         uint64_t freespace = UINT64_MAX, mda_free;
219         const char *pvid = (const char *)&pv->id.uuid;
220         struct metadata_area *mda;
221
222         if ((info = info_from_pvid(pvid, 0)))
223                 dm_list_iterate_items(mda, &info->mdas) {
224                         if (!mda->ops->mda_free_sectors)
225                                 continue;
226                         mda_free = mda->ops->mda_free_sectors(mda);
227                         if (mda_free < freespace)
228                                 freespace = mda_free;
229                 }
230
231         if (freespace == UINT64_MAX)
232                 freespace = UINT64_C(0);
233         return freespace;
234 }
235
236 uint64_t pv_used(const struct physical_volume *pv)
237 {
238         uint64_t used;
239
240         if (!pv->pe_count)
241                 used = 0LL;
242         else
243                 used = (uint64_t) pv->pe_alloc_count * pv->pe_size;
244         return used;
245 }
246
247 unsigned pv_mda_set_ignored(const struct physical_volume *pv, unsigned mda_ignored)
248 {
249         struct lvmcache_info *info;
250         struct metadata_area *mda, *vg_mda, *tmda;
251         struct dm_list *vg_mdas_in_use, *vg_mdas_ignored;
252
253         if (!(info = info_from_pvid((const char *)&pv->id.uuid, 0)))
254                 return_0;
255
256         if (is_orphan(pv)) {
257                 dm_list_iterate_items(mda, &info->mdas)
258                         mda_set_ignored(mda, mda_ignored);
259                 return 1;
260         }
261
262         /*
263          * Do not allow disabling of the the last PV in a VG.
264          */
265         if (pv_mda_used_count(pv) == vg_mda_used_count(pv->vg)) {
266                 log_error("Cannot disable all metadata areas in volume group %s.",
267                           pv->vg->name);
268                 return 0;
269         }
270
271         /*
272          * Non-orphan case is more complex.
273          * If the PV's mdas are ignored, and we wish to un-ignore,
274          * we clear the bit and move them from the ignored mda list to the
275          * in_use list, ensuring the new state will get written to disk
276          * in the vg_write() path.
277          * If the PV's mdas are not ignored, and we are setting
278          * them to ignored, we set the bit but leave them on the in_use
279          * list, ensuring the new state will get written to disk in the
280          * vg_write() path.
281          */
282         vg_mdas_in_use = &pv->vg->fid->metadata_areas_in_use;
283         vg_mdas_ignored = &pv->vg->fid->metadata_areas_ignored;
284
285         dm_list_iterate_items(mda, &info->mdas) {
286                 if (mda_is_ignored(mda) && !mda_ignored)
287                         /* Changing an ignored mda to one in_use requires moving it */
288                         dm_list_iterate_items_safe(vg_mda, tmda, vg_mdas_ignored)
289                                 if (mda_locns_match(mda, vg_mda)) {
290                                         mda_set_ignored(vg_mda, mda_ignored);
291                                         dm_list_move(vg_mdas_in_use, &vg_mda->list);
292                                 }
293
294                 dm_list_iterate_items_safe(vg_mda, tmda, vg_mdas_in_use)
295                         if (mda_locns_match(mda, vg_mda))
296                                 /* Don't move mda: needs writing to disk. */
297                                 mda_set_ignored(vg_mda, mda_ignored);
298
299                 mda_set_ignored(mda, mda_ignored);
300         }
301
302         return 1;
303 }
304